management 1.3.2 → 1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -10
- data/lib/management.rb +10 -10
- data/lib/management/command.rb +14 -6
- data/lib/management/commands/address/attach.rb +24 -0
- data/lib/management/commands/address/list.rb +35 -0
- data/lib/management/commands/server/console.rb +20 -0
- data/lib/management/commands/server/create.rb +52 -0
- data/lib/management/commands/server/destroy.rb +31 -0
- data/lib/management/commands/server/list.rb +44 -0
- data/lib/management/commands/server/run.rb +126 -0
- data/lib/management/commands/server/ssh.rb +23 -0
- data/lib/management/commands/server/start.rb +22 -0
- data/lib/management/commands/server/stop.rb +22 -0
- data/lib/management/version.rb +1 -1
- metadata +11 -11
- data/lib/management/commands/attach_address.rb +0 -20
- data/lib/management/commands/create_server.rb +0 -48
- data/lib/management/commands/destroy_servers.rb +0 -27
- data/lib/management/commands/list_addresses.rb +0 -31
- data/lib/management/commands/list_servers.rb +0 -40
- data/lib/management/commands/open_console.rb +0 -16
- data/lib/management/commands/run_script.rb +0 -122
- data/lib/management/commands/ssh_server.rb +0 -19
- data/lib/management/commands/start_server.rb +0 -18
- data/lib/management/commands/stop_server.rb +0 -18
data/README.md
CHANGED
@@ -18,16 +18,16 @@ Minimalist EC2 configuration & deployment tool.
|
|
18
18
|
Usage: management [command [arg ...]]
|
19
19
|
|
20
20
|
Commands:
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
21
|
+
server:create <env> <type>
|
22
|
+
server:list [<env>]
|
23
|
+
server:destroy <server> [...]
|
24
|
+
server:start <server>
|
25
|
+
server:stop <server>
|
26
|
+
server:run <server> <script>
|
27
|
+
server:ssh <server>
|
28
|
+
server:console
|
29
|
+
address:list
|
30
|
+
address:attach <address> <server>
|
31
31
|
|
32
32
|
-h, --help Display this screen
|
33
33
|
-v, --version Show version
|
data/lib/management.rb
CHANGED
@@ -2,13 +2,13 @@ require_relative 'management/version'
|
|
2
2
|
require_relative 'management/interpreter'
|
3
3
|
require_relative 'management/helper'
|
4
4
|
require_relative 'management/command'
|
5
|
-
require_relative 'management/commands/
|
6
|
-
require_relative 'management/commands/
|
7
|
-
require_relative 'management/commands/
|
8
|
-
require_relative 'management/commands/
|
9
|
-
require_relative 'management/commands/
|
10
|
-
require_relative 'management/commands/
|
11
|
-
require_relative 'management/commands/
|
12
|
-
require_relative 'management/commands/
|
13
|
-
require_relative 'management/commands/
|
14
|
-
require_relative 'management/commands/
|
5
|
+
require_relative 'management/commands/server/create'
|
6
|
+
require_relative 'management/commands/server/list'
|
7
|
+
require_relative 'management/commands/server/destroy'
|
8
|
+
require_relative 'management/commands/server/start'
|
9
|
+
require_relative 'management/commands/server/stop'
|
10
|
+
require_relative 'management/commands/server/run'
|
11
|
+
require_relative 'management/commands/server/ssh'
|
12
|
+
require_relative 'management/commands/server/console'
|
13
|
+
require_relative 'management/commands/address/list'
|
14
|
+
require_relative 'management/commands/address/attach'
|
data/lib/management/command.rb
CHANGED
@@ -10,20 +10,24 @@ module Management
|
|
10
10
|
all << subclass.new
|
11
11
|
end
|
12
12
|
|
13
|
+
def self.maxlen
|
14
|
+
all.map(&:command_name).map(&:size).max
|
15
|
+
end
|
16
|
+
|
13
17
|
def fn
|
14
18
|
method(:run)
|
15
19
|
end
|
16
20
|
|
17
21
|
def command_name
|
18
|
-
self.class.name.
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
+
self.class.name.
|
23
|
+
split('::').
|
24
|
+
drop(1).
|
25
|
+
join(":").
|
22
26
|
downcase
|
23
27
|
end
|
24
28
|
|
25
|
-
def
|
26
|
-
|
29
|
+
def arg_list
|
30
|
+
fn.parameters.map do |req, name|
|
27
31
|
name = '<' + name.to_s.sub(/_names?/, '') + '>'
|
28
32
|
case req
|
29
33
|
when :opt then '[' + name + ']'
|
@@ -33,6 +37,10 @@ module Management
|
|
33
37
|
end.join(' ')
|
34
38
|
end
|
35
39
|
|
40
|
+
def help_string
|
41
|
+
sprintf " %-#{Command.maxlen + 2}s %s", command_name, arg_list
|
42
|
+
end
|
43
|
+
|
36
44
|
def true_arity
|
37
45
|
min = fn.parameters.take_while{|req, name| req == :req}.count
|
38
46
|
max = fn.parameters.count
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative '../../command'
|
2
|
+
|
3
|
+
module Management
|
4
|
+
|
5
|
+
module Address
|
6
|
+
|
7
|
+
class Attach < Management::Command
|
8
|
+
|
9
|
+
include Management::Helper
|
10
|
+
|
11
|
+
def run(address_name, server_name)
|
12
|
+
address = get_address(address_name)
|
13
|
+
server = get_server(server_name)
|
14
|
+
|
15
|
+
puts "Attaching #{address_name} to #{server_name}..."
|
16
|
+
address.server = server
|
17
|
+
puts "Done."
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative '../../command'
|
2
|
+
|
3
|
+
module Management
|
4
|
+
|
5
|
+
module Address
|
6
|
+
|
7
|
+
class List < Management::Command
|
8
|
+
|
9
|
+
include Management::Helper
|
10
|
+
|
11
|
+
def run()
|
12
|
+
servers = live_servers
|
13
|
+
|
14
|
+
cols = [
|
15
|
+
{size: 20, title: "IP", fn: ->(addr){ addr.public_ip } },
|
16
|
+
{size: 20, title: "Server", fn: ->(addr){ s = servers.find{|server| server.id == addr.server_id }; s ? s.name : "n/a" } },
|
17
|
+
{size: 30, title: "Name", fn: ->(addr){ a = config[:addresses].find{|k, v| v == addr.public_ip}; a ? a.first : "n/a" } },
|
18
|
+
{size: 15, title: "Status", fn: ->(addr){ s = servers.find{|server| server.id == addr.server_id }; s ? s.state : "n/a" } },
|
19
|
+
]
|
20
|
+
|
21
|
+
format = cols.map{|c| "%-#{c[:size]}s"}.join(" ") + "\n"
|
22
|
+
|
23
|
+
send :printf, *([format].concat(cols.map{|c|c[:title]}))
|
24
|
+
send :printf, *([format].concat(cols.map{|c|'-' * c[:size]}))
|
25
|
+
|
26
|
+
cloud.addresses.each do |address|
|
27
|
+
send :printf, *([format].concat(cols.map{|c|c[:fn].call address}))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require_relative '../../command'
|
2
|
+
|
3
|
+
module Management
|
4
|
+
|
5
|
+
module Server
|
6
|
+
|
7
|
+
class Create < Management::Command
|
8
|
+
|
9
|
+
include Management::Helper
|
10
|
+
|
11
|
+
def run(env_name, type_name)
|
12
|
+
env = get_env(env_name)
|
13
|
+
type = get_type(type_name)
|
14
|
+
|
15
|
+
servers = live_servers
|
16
|
+
name = make_unique_server_name(env_name, type_name, servers)
|
17
|
+
|
18
|
+
puts "Creating \"#{name}\"..."
|
19
|
+
|
20
|
+
cloud.servers.create(type.merge({tags: {
|
21
|
+
"Creator" => current_user,
|
22
|
+
"CreatedAt" => Time.new.strftime("%Y%m%d%H%M%S"),
|
23
|
+
"Name" => name,
|
24
|
+
"Env" => env_name,
|
25
|
+
"Meal" => type_name,
|
26
|
+
}}))
|
27
|
+
|
28
|
+
puts "Done."
|
29
|
+
end
|
30
|
+
|
31
|
+
def current_user
|
32
|
+
`git config user.name`.strip
|
33
|
+
rescue
|
34
|
+
"unknown"
|
35
|
+
end
|
36
|
+
|
37
|
+
def make_unique_server_name(env_name, type_name, servers)
|
38
|
+
(1..Float::INFINITY).each do |i|
|
39
|
+
name = "#{env_name}-#{type_name}-#{i}"
|
40
|
+
if servers.find{|s|s.name == name}
|
41
|
+
i += 1
|
42
|
+
else
|
43
|
+
return name
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative '../../command'
|
2
|
+
|
3
|
+
module Management
|
4
|
+
|
5
|
+
module Server
|
6
|
+
|
7
|
+
class Destroy < Management::Command
|
8
|
+
|
9
|
+
include Management::Helper
|
10
|
+
|
11
|
+
def run(*server_names)
|
12
|
+
servers = server_names.map{|server_name| get_server(server_name)}
|
13
|
+
|
14
|
+
puts "You are about to delete the following servers:"
|
15
|
+
puts ['', *servers.map{ |server| " - #{server.name}" }, '', ''].join("\n")
|
16
|
+
|
17
|
+
print "Are you sure you want to do this? Type 'Yes' to continue, or anything else to abort: "
|
18
|
+
abort "Aborted." if $stdin.gets.chomp != 'Yes'
|
19
|
+
|
20
|
+
servers.each do |server|
|
21
|
+
puts "Destroying #{server.name}..."
|
22
|
+
server.destroy
|
23
|
+
puts "Done."
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require_relative '../../command'
|
2
|
+
|
3
|
+
module Management
|
4
|
+
|
5
|
+
module Server
|
6
|
+
|
7
|
+
class List < Management::Command
|
8
|
+
|
9
|
+
include Management::Helper
|
10
|
+
|
11
|
+
def run(env_name = nil)
|
12
|
+
env = get_env(env_name)
|
13
|
+
|
14
|
+
cols = [
|
15
|
+
{size: 20, title: "Name", field: :name },
|
16
|
+
{size: 10, title: "State", field: :state },
|
17
|
+
{size: 20, title: "IP", field: :public_ip_address },
|
18
|
+
{size: 20, title: "Private IP", field: :private_ip_address },
|
19
|
+
{size: 10, title: "Size", field: :flavor_id },
|
20
|
+
{size: 15, title: "Env", field: :env },
|
21
|
+
{size: 15, title: "Type", field: :type },
|
22
|
+
{size: 11, title: "EC2 ID", field: :id },
|
23
|
+
]
|
24
|
+
|
25
|
+
format = cols.map{|c| "%-#{c[:size]}s"}.join(" ") + "\n"
|
26
|
+
|
27
|
+
send :printf, *([format].concat(cols.map{|c|c[:title]}))
|
28
|
+
send :printf, *([format].concat(cols.map{|c|'-' * c[:size]}))
|
29
|
+
|
30
|
+
servers = live_servers.sort_by(&:name)
|
31
|
+
|
32
|
+
servers.each do |server|
|
33
|
+
next if env_name && server.env != env_name
|
34
|
+
next if server.state == 'terminated'
|
35
|
+
|
36
|
+
send :printf, *([format].concat(cols.map{|c|server.send(c[:field])}))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require_relative '../../command'
|
2
|
+
|
3
|
+
module Management
|
4
|
+
|
5
|
+
module Server
|
6
|
+
|
7
|
+
class Run < Management::Command
|
8
|
+
|
9
|
+
include Management::Helper
|
10
|
+
|
11
|
+
def run(server_name, script_name)
|
12
|
+
require 'tmpdir'
|
13
|
+
require 'fileutils'
|
14
|
+
require 'erb'
|
15
|
+
require 'shellwords'
|
16
|
+
|
17
|
+
server = get_server(server_name)
|
18
|
+
script = get_script(script_name)
|
19
|
+
|
20
|
+
server.private_key_path = config[:ssh_key_path]
|
21
|
+
|
22
|
+
missing = missing_local_files(script)
|
23
|
+
abort "The following files are missing:" + (["\n"] + missing).join("\n - ") if !missing.empty?
|
24
|
+
|
25
|
+
script.each do |tuple|
|
26
|
+
type, data = *tuple.first
|
27
|
+
|
28
|
+
case type.to_sym
|
29
|
+
when :copy
|
30
|
+
local_path, remote_path, opts = *data
|
31
|
+
puts "############ Copying #{local_path} -> #{remote_path}"
|
32
|
+
|
33
|
+
copy_file(server, local_path, remote_path, opts)
|
34
|
+
when :run
|
35
|
+
cmd = data
|
36
|
+
puts "############ Running #{cmd}"
|
37
|
+
|
38
|
+
code = run_remote_command(server, cmd)
|
39
|
+
if code != 0
|
40
|
+
abort "############ Failed. Exit code: #{code}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
puts "############ Success!"
|
46
|
+
end
|
47
|
+
|
48
|
+
def copy_file(server, local_path, remote_path, opts = nil)
|
49
|
+
should_template = opts && opts[:template]
|
50
|
+
custom_chown = opts && opts[:chown]
|
51
|
+
custom_chmod = opts && opts[:chmod]
|
52
|
+
|
53
|
+
Dir.mktmpdir('management-file-dir') do |file_tmpdir|
|
54
|
+
|
55
|
+
# copy to the fake "remote" path locally
|
56
|
+
remote_looking_path = File.join(file_tmpdir, remote_path)
|
57
|
+
FileUtils.mkdir_p File.dirname(remote_looking_path)
|
58
|
+
FileUtils.cp_r local_path, remote_looking_path, preserve: true
|
59
|
+
|
60
|
+
# overwrite the fake "remote" file with its own templated contents if necessary
|
61
|
+
if should_template
|
62
|
+
new_contents = ERB.new(File.read(remote_looking_path)).result(binding)
|
63
|
+
File.write(remote_looking_path, new_contents)
|
64
|
+
end
|
65
|
+
|
66
|
+
Dir.mktmpdir('management-tar-dir') do |tar_tmpdir|
|
67
|
+
|
68
|
+
# zip this file up, starting from its absolute path
|
69
|
+
local_tar_path = File.join(tar_tmpdir, "__management__.tar.gz")
|
70
|
+
zip_relevant_files(file_tmpdir, local_tar_path)
|
71
|
+
|
72
|
+
# copy tar file to remote and extract
|
73
|
+
remote_tar_path = "/tmp/__management__.tar.gz"
|
74
|
+
server.copy_file(local_tar_path, remote_tar_path)
|
75
|
+
server.extract_tar(remote_tar_path)
|
76
|
+
server.chown_r(remote_path, custom_chown) if custom_chown
|
77
|
+
server.chmod(remote_path, custom_chmod) if custom_chmod
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# returns error code
|
83
|
+
def run_remote_command(server, cmd)
|
84
|
+
result = server.ssh("#{cmd}").first
|
85
|
+
return result.status
|
86
|
+
end
|
87
|
+
|
88
|
+
def missing_local_files(script)
|
89
|
+
script.find_all do |tuple|
|
90
|
+
type, data = *tuple.first
|
91
|
+
if type == :copy
|
92
|
+
local, remote = *data
|
93
|
+
! File.exists?(local)
|
94
|
+
end
|
95
|
+
end.map do |tuple|
|
96
|
+
type, data = *tuple.first
|
97
|
+
local, remote = *data
|
98
|
+
local
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def relevant_files(at_dir)
|
103
|
+
abort unless at_dir.start_with? "/"
|
104
|
+
|
105
|
+
Dir[File.join(at_dir, "**/*")].select do |path|
|
106
|
+
File.file?(path) || (File.directory?(path) && Dir.entries(path) == [".", ".."])
|
107
|
+
end.map do |path|
|
108
|
+
path.slice! at_dir.end_with?("/") ? at_dir : "#{at_dir}/"
|
109
|
+
"./#{path}"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
def zip_relevant_files(in_dir, out_file)
|
116
|
+
Dir.chdir(in_dir) do
|
117
|
+
file_list = Shellwords.join(relevant_files(in_dir))
|
118
|
+
system("tar -czf #{out_file} #{file_list}")
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative '../../command'
|
2
|
+
|
3
|
+
module Management
|
4
|
+
|
5
|
+
module Server
|
6
|
+
|
7
|
+
class Ssh < Management::Command
|
8
|
+
|
9
|
+
include Management::Helper
|
10
|
+
|
11
|
+
def run(server_name)
|
12
|
+
server = get_server(server_name)
|
13
|
+
|
14
|
+
ssh_key_path = config[:ssh_key_path]
|
15
|
+
system_verbose "chmod 0600 #{ssh_key_path}"
|
16
|
+
system_verbose "ssh -i #{ssh_key_path} #{config[:root_user]}@#{server.public_ip_address}"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative '../../command'
|
2
|
+
|
3
|
+
module Management
|
4
|
+
|
5
|
+
module Server
|
6
|
+
|
7
|
+
class Start < Management::Command
|
8
|
+
|
9
|
+
include Management::Helper
|
10
|
+
|
11
|
+
def run(server_name)
|
12
|
+
server = get_server(server_name)
|
13
|
+
puts "Starting #{server_name}..."
|
14
|
+
server.start
|
15
|
+
puts "Done."
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative '../../command'
|
2
|
+
|
3
|
+
module Management
|
4
|
+
|
5
|
+
module Server
|
6
|
+
|
7
|
+
class Stop < Management::Command
|
8
|
+
|
9
|
+
include Management::Helper
|
10
|
+
|
11
|
+
def run(server_name)
|
12
|
+
server = get_server(server_name)
|
13
|
+
puts "Stopping #{server_name}..."
|
14
|
+
server.stop
|
15
|
+
puts "Done."
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/lib/management/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: management
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: '1.4'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -140,16 +140,16 @@ files:
|
|
140
140
|
- lib/ext/fog.rb
|
141
141
|
- lib/management.rb
|
142
142
|
- lib/management/command.rb
|
143
|
-
- lib/management/commands/
|
144
|
-
- lib/management/commands/
|
145
|
-
- lib/management/commands/
|
146
|
-
- lib/management/commands/
|
147
|
-
- lib/management/commands/
|
148
|
-
- lib/management/commands/
|
149
|
-
- lib/management/commands/
|
150
|
-
- lib/management/commands/
|
151
|
-
- lib/management/commands/
|
152
|
-
- lib/management/commands/
|
143
|
+
- lib/management/commands/address/attach.rb
|
144
|
+
- lib/management/commands/address/list.rb
|
145
|
+
- lib/management/commands/server/console.rb
|
146
|
+
- lib/management/commands/server/create.rb
|
147
|
+
- lib/management/commands/server/destroy.rb
|
148
|
+
- lib/management/commands/server/list.rb
|
149
|
+
- lib/management/commands/server/run.rb
|
150
|
+
- lib/management/commands/server/ssh.rb
|
151
|
+
- lib/management/commands/server/start.rb
|
152
|
+
- lib/management/commands/server/stop.rb
|
153
153
|
- lib/management/helper.rb
|
154
154
|
- lib/management/interpreter.rb
|
155
155
|
- lib/management/version.rb
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require_relative '../command'
|
2
|
-
|
3
|
-
module Management
|
4
|
-
|
5
|
-
class AttachAddress < Management::Command
|
6
|
-
|
7
|
-
include Management::Helper
|
8
|
-
|
9
|
-
def run(address_name, server_name)
|
10
|
-
address = get_address(address_name)
|
11
|
-
server = get_server(server_name)
|
12
|
-
|
13
|
-
puts "Attaching #{address_name} to #{server_name}..."
|
14
|
-
address.server = server
|
15
|
-
puts "Done."
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
require_relative '../command'
|
2
|
-
|
3
|
-
module Management
|
4
|
-
|
5
|
-
class CreateServer < Management::Command
|
6
|
-
|
7
|
-
include Management::Helper
|
8
|
-
|
9
|
-
def run(env_name, type_name)
|
10
|
-
env = get_env(env_name)
|
11
|
-
type = get_type(type_name)
|
12
|
-
|
13
|
-
servers = live_servers
|
14
|
-
name = make_unique_server_name(env_name, type_name, servers)
|
15
|
-
|
16
|
-
puts "Creating \"#{name}\"..."
|
17
|
-
|
18
|
-
cloud.servers.create(type.merge({tags: {
|
19
|
-
"Creator" => current_user,
|
20
|
-
"CreatedAt" => Time.new.strftime("%Y%m%d%H%M%S"),
|
21
|
-
"Name" => name,
|
22
|
-
"Env" => env_name,
|
23
|
-
"Meal" => type_name,
|
24
|
-
}}))
|
25
|
-
|
26
|
-
puts "Done."
|
27
|
-
end
|
28
|
-
|
29
|
-
def current_user
|
30
|
-
`git config user.name`.strip
|
31
|
-
rescue
|
32
|
-
"unknown"
|
33
|
-
end
|
34
|
-
|
35
|
-
def make_unique_server_name(env_name, type_name, servers)
|
36
|
-
(1..Float::INFINITY).each do |i|
|
37
|
-
name = "#{env_name}-#{type_name}-#{i}"
|
38
|
-
if servers.find{|s|s.name == name}
|
39
|
-
i += 1
|
40
|
-
else
|
41
|
-
return name
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require_relative '../command'
|
2
|
-
|
3
|
-
module Management
|
4
|
-
|
5
|
-
class DestroyServers < Management::Command
|
6
|
-
|
7
|
-
include Management::Helper
|
8
|
-
|
9
|
-
def run(*server_names)
|
10
|
-
servers = server_names.map{|server_name| get_server(server_name)}
|
11
|
-
|
12
|
-
puts "You are about to delete the following servers:"
|
13
|
-
puts ['', *servers.map{ |server| " - #{server.name}" }, '', ''].join("\n")
|
14
|
-
|
15
|
-
print "Are you sure you want to do this? Type 'Yes' to continue, or anything else to abort: "
|
16
|
-
abort "Aborted." if $stdin.gets.chomp != 'Yes'
|
17
|
-
|
18
|
-
servers.each do |server|
|
19
|
-
puts "Destroying #{server.name}..."
|
20
|
-
server.destroy
|
21
|
-
puts "Done."
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require_relative '../command'
|
2
|
-
|
3
|
-
module Management
|
4
|
-
|
5
|
-
class ListAddresses < Management::Command
|
6
|
-
|
7
|
-
include Management::Helper
|
8
|
-
|
9
|
-
def run()
|
10
|
-
servers = live_servers
|
11
|
-
|
12
|
-
cols = [
|
13
|
-
{size: 20, title: "IP", fn: ->(addr){ addr.public_ip } },
|
14
|
-
{size: 20, title: "Server", fn: ->(addr){ s = servers.find{|server| server.id == addr.server_id }; s ? s.name : "n/a" } },
|
15
|
-
{size: 30, title: "Name", fn: ->(addr){ a = config[:addresses].find{|k, v| v == addr.public_ip}; a ? a.first : "n/a" } },
|
16
|
-
{size: 15, title: "Status", fn: ->(addr){ s = servers.find{|server| server.id == addr.server_id }; s ? s.state : "n/a" } },
|
17
|
-
]
|
18
|
-
|
19
|
-
format = cols.map{|c| "%-#{c[:size]}s"}.join(" ") + "\n"
|
20
|
-
|
21
|
-
send :printf, *([format].concat(cols.map{|c|c[:title]}))
|
22
|
-
send :printf, *([format].concat(cols.map{|c|'-' * c[:size]}))
|
23
|
-
|
24
|
-
cloud.addresses.each do |address|
|
25
|
-
send :printf, *([format].concat(cols.map{|c|c[:fn].call address}))
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require_relative '../command'
|
2
|
-
|
3
|
-
module Management
|
4
|
-
|
5
|
-
class ListServers < Management::Command
|
6
|
-
|
7
|
-
include Management::Helper
|
8
|
-
|
9
|
-
def run(env_name = nil)
|
10
|
-
env = get_env(env_name)
|
11
|
-
|
12
|
-
cols = [
|
13
|
-
{size: 20, title: "Name", field: :name },
|
14
|
-
{size: 10, title: "State", field: :state },
|
15
|
-
{size: 20, title: "IP", field: :public_ip_address },
|
16
|
-
{size: 20, title: "Private IP", field: :private_ip_address },
|
17
|
-
{size: 10, title: "Size", field: :flavor_id },
|
18
|
-
{size: 15, title: "Env", field: :env },
|
19
|
-
{size: 15, title: "Type", field: :type },
|
20
|
-
{size: 11, title: "EC2 ID", field: :id },
|
21
|
-
]
|
22
|
-
|
23
|
-
format = cols.map{|c| "%-#{c[:size]}s"}.join(" ") + "\n"
|
24
|
-
|
25
|
-
send :printf, *([format].concat(cols.map{|c|c[:title]}))
|
26
|
-
send :printf, *([format].concat(cols.map{|c|'-' * c[:size]}))
|
27
|
-
|
28
|
-
servers = live_servers.sort_by(&:name)
|
29
|
-
|
30
|
-
servers.each do |server|
|
31
|
-
next if env_name && server.env != env_name
|
32
|
-
next if server.state == 'terminated'
|
33
|
-
|
34
|
-
send :printf, *([format].concat(cols.map{|c|server.send(c[:field])}))
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
@@ -1,122 +0,0 @@
|
|
1
|
-
require_relative '../command'
|
2
|
-
|
3
|
-
module Management
|
4
|
-
|
5
|
-
class RunScript < Management::Command
|
6
|
-
|
7
|
-
include Management::Helper
|
8
|
-
|
9
|
-
def run(server_name, script_name)
|
10
|
-
require 'tmpdir'
|
11
|
-
require 'fileutils'
|
12
|
-
require 'erb'
|
13
|
-
require 'shellwords'
|
14
|
-
|
15
|
-
server = get_server(server_name)
|
16
|
-
script = get_script(script_name)
|
17
|
-
|
18
|
-
server.private_key_path = config[:ssh_key_path]
|
19
|
-
|
20
|
-
missing = missing_local_files(script)
|
21
|
-
abort "The following files are missing:" + (["\n"] + missing).join("\n - ") if !missing.empty?
|
22
|
-
|
23
|
-
script.each do |tuple|
|
24
|
-
type, data = *tuple.first
|
25
|
-
|
26
|
-
case type.to_sym
|
27
|
-
when :copy
|
28
|
-
local_path, remote_path, opts = *data
|
29
|
-
puts "############ Copying #{local_path} -> #{remote_path}"
|
30
|
-
|
31
|
-
copy_file(server, local_path, remote_path, opts)
|
32
|
-
when :run
|
33
|
-
cmd = data
|
34
|
-
puts "############ Running #{cmd}"
|
35
|
-
|
36
|
-
code = run_remote_command(server, cmd)
|
37
|
-
if code != 0
|
38
|
-
abort "############ Failed. Exit code: #{code}"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
puts "############ Success!"
|
44
|
-
end
|
45
|
-
|
46
|
-
def copy_file(server, local_path, remote_path, opts = nil)
|
47
|
-
should_template = opts && opts[:template]
|
48
|
-
custom_chown = opts && opts[:chown]
|
49
|
-
custom_chmod = opts && opts[:chmod]
|
50
|
-
|
51
|
-
Dir.mktmpdir('management-file-dir') do |file_tmpdir|
|
52
|
-
|
53
|
-
# copy to the fake "remote" path locally
|
54
|
-
remote_looking_path = File.join(file_tmpdir, remote_path)
|
55
|
-
FileUtils.mkdir_p File.dirname(remote_looking_path)
|
56
|
-
FileUtils.cp_r local_path, remote_looking_path, preserve: true
|
57
|
-
|
58
|
-
# overwrite the fake "remote" file with its own templated contents if necessary
|
59
|
-
if should_template
|
60
|
-
new_contents = ERB.new(File.read(remote_looking_path)).result(binding)
|
61
|
-
File.write(remote_looking_path, new_contents)
|
62
|
-
end
|
63
|
-
|
64
|
-
Dir.mktmpdir('management-tar-dir') do |tar_tmpdir|
|
65
|
-
|
66
|
-
# zip this file up, starting from its absolute path
|
67
|
-
local_tar_path = File.join(tar_tmpdir, "__management__.tar.gz")
|
68
|
-
zip_relevant_files(file_tmpdir, local_tar_path)
|
69
|
-
|
70
|
-
# copy tar file to remote and extract
|
71
|
-
remote_tar_path = "/tmp/__management__.tar.gz"
|
72
|
-
server.copy_file(local_tar_path, remote_tar_path)
|
73
|
-
server.extract_tar(remote_tar_path)
|
74
|
-
server.chown_r(remote_path, custom_chown) if custom_chown
|
75
|
-
server.chmod(remote_path, custom_chmod) if custom_chmod
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
# returns error code
|
81
|
-
def run_remote_command(server, cmd)
|
82
|
-
result = server.ssh("#{cmd}").first
|
83
|
-
return result.status
|
84
|
-
end
|
85
|
-
|
86
|
-
def missing_local_files(script)
|
87
|
-
script.find_all do |tuple|
|
88
|
-
type, data = *tuple.first
|
89
|
-
if type == :copy
|
90
|
-
local, remote = *data
|
91
|
-
! File.exists?(local)
|
92
|
-
end
|
93
|
-
end.map do |tuple|
|
94
|
-
type, data = *tuple.first
|
95
|
-
local, remote = *data
|
96
|
-
local
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
def relevant_files(at_dir)
|
101
|
-
abort unless at_dir.start_with? "/"
|
102
|
-
|
103
|
-
Dir[File.join(at_dir, "**/*")].select do |path|
|
104
|
-
File.file?(path) || (File.directory?(path) && Dir.entries(path) == [".", ".."])
|
105
|
-
end.map do |path|
|
106
|
-
path.slice! at_dir.end_with?("/") ? at_dir : "#{at_dir}/"
|
107
|
-
"./#{path}"
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
private
|
112
|
-
|
113
|
-
def zip_relevant_files(in_dir, out_file)
|
114
|
-
Dir.chdir(in_dir) do
|
115
|
-
file_list = Shellwords.join(relevant_files(in_dir))
|
116
|
-
system("tar -czf #{out_file} #{file_list}")
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
end
|
121
|
-
|
122
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require_relative '../command'
|
2
|
-
|
3
|
-
module Management
|
4
|
-
|
5
|
-
class SshServer < Management::Command
|
6
|
-
|
7
|
-
include Management::Helper
|
8
|
-
|
9
|
-
def run(server_name)
|
10
|
-
server = get_server(server_name)
|
11
|
-
|
12
|
-
ssh_key_path = config[:ssh_key_path]
|
13
|
-
system_verbose "chmod 0600 #{ssh_key_path}"
|
14
|
-
system_verbose "ssh -i #{ssh_key_path} #{config[:root_user]}@#{server.public_ip_address}"
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
require_relative '../command'
|
2
|
-
|
3
|
-
module Management
|
4
|
-
|
5
|
-
class StartServer < Management::Command
|
6
|
-
|
7
|
-
include Management::Helper
|
8
|
-
|
9
|
-
def run(server_name)
|
10
|
-
server = get_server(server_name)
|
11
|
-
puts "Starting #{server_name}..."
|
12
|
-
server.start
|
13
|
-
puts "Done."
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
require_relative '../command'
|
2
|
-
|
3
|
-
module Management
|
4
|
-
|
5
|
-
class StopServer < Management::Command
|
6
|
-
|
7
|
-
include Management::Helper
|
8
|
-
|
9
|
-
def run(server_name)
|
10
|
-
server = get_server(server_name)
|
11
|
-
puts "Stopping #{server_name}..."
|
12
|
-
server.stop
|
13
|
-
puts "Done."
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|