sistero 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 35f35cc9623defac9aa56a5001ea501efb8cb1d8
4
- data.tar.gz: 337014699bacd1fff480765b9d9406c0f0664be6
3
+ metadata.gz: adc5b3b7bea1e58657bbf47ddffd5e3efa69c6c1
4
+ data.tar.gz: c77fbaf01f32c5833db5960c6cb52b7acc0f92fe
5
5
  SHA512:
6
- metadata.gz: a20d70f01af21bd2c091ba9cbe5099dcf3b1c6edef4c0420fc4a73e72df0f2e726f3cb55fa367b2e69efeb332e641bf048a48cc3d54f45b7857b70ded6c64673
7
- data.tar.gz: 30735f69d59cab88b7b7c0355e3b5a934b501a20d8fe7b52ab1c78b0d8056be3b1ba5ce8671b4180309917550b9e699e56d925fc340c304f34043646c7ab6b6b
6
+ metadata.gz: faec4b54c8ff3d797f16c00d67e6d21598d69ade190ed600205f07823849f0ba104eace69870cc2a2be5de8b5413ac5f89b3132e1cb391c3d0179548e23ab14e
7
+ data.tar.gz: 7e0e6901c61589863bbf1a6f23f81e9dbc607b17e04f749ac91a61deccb950ac49722a39d90107b6fd708611b1860d1ce5801db06eb535773eadc4ea88242064
data/README.md CHANGED
@@ -13,7 +13,7 @@ Create a config file at `.config/sistero` with something like:
13
13
  ```
14
14
  ---
15
15
  defaults:
16
- vm_name: my_default_vm_name
16
+ vm_name: default-vm
17
17
  ssh_keys:
18
18
  - 1234567
19
19
  ssh_options: -D1080 -A
@@ -21,19 +21,24 @@ defaults:
21
21
  vm_size: 512mb
22
22
  vm_region: nyc3
23
23
  vm_image: ubuntu-14-04-x64
24
- london:
25
- vm_name: my_london_vm_name
26
- vm_region: lon1
24
+ profiles:
25
+ -
26
+ vm_name: london-vm
27
+ vm_region: lon1
28
+ -
29
+ vm_name: london-bigger-vm
30
+ vm_region: lon1
31
+ vm_size: 1gb
27
32
  ```
28
33
 
29
34
  The `ssh_keys` is a list of the ID numbers of your ssh keys.
30
35
 
31
- Then to create a VM in new york:
36
+ Then to create a VM in new york called default-vm:
32
37
  ```
33
38
  sistero create
34
39
  ```
35
40
 
36
- Or to create a VM configured the same only in London:
41
+ Or to create a VM called london-vm in london:
37
42
  ```
38
- sistero -p london create
43
+ sistero -n london create
39
44
  ```
data/bin/sistero CHANGED
@@ -46,8 +46,8 @@ module Sistero::Command
46
46
  exit
47
47
  end
48
48
 
49
- op.on '-p val', '--profile', 'set profile' do |profile_name|
50
- @config.profile_name = profile_name
49
+ op.on '-n vm-name', '--name', 'set vm name' do |vm_name|
50
+ @config.vm_name = vm_name
51
51
  end
52
52
 
53
53
  subcommand 'ssh', 'ssh to vm' do |subop|
@@ -62,6 +62,8 @@ module Sistero::Command
62
62
 
63
63
  subcommand 'list', 'list vms'
64
64
 
65
+ subcommand 'show-config', 'show configuration'
66
+
65
67
  op.parse!
66
68
  end
67
69
 
@@ -70,16 +72,24 @@ module Sistero::Command
70
72
  exit
71
73
  end
72
74
 
73
- sistero = Sistero::Instance.new
74
- case @config.action
75
- when 'ssh'
76
- sistero.ssh_to_vm(@config.profile_name, ssh_options: @config.ssh_options)
77
- when 'create'
78
- sistero.create_vm(@config.profile_name)
79
- when 'destroy'
80
- sistero.destroy_vm(@config.profile_name)
81
- when 'list'
82
- sistero.list_vms()
75
+ vm_name = @config.vm_name
76
+
77
+ begin
78
+ sistero = Sistero::Instance.new
79
+ case @config.action
80
+ when 'ssh'
81
+ sistero.ssh_to_vm(vm_name, ssh_options: @config.ssh_options)
82
+ when 'create'
83
+ sistero.create_vm(vm_name)
84
+ when 'destroy'
85
+ sistero.destroy_vm(vm_name)
86
+ when 'list'
87
+ sistero.list_vms()
88
+ when 'show-config'
89
+ sistero.show_config()
90
+ end
91
+ rescue RuntimeError => e
92
+ puts e.to_s
83
93
  end
84
94
  end
85
95
  end
@@ -1,3 +1,3 @@
1
1
  module Sistero
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/sistero.rb CHANGED
@@ -5,42 +5,61 @@ require "droplet_kit"
5
5
  APP_NAME = "sistero"
6
6
 
7
7
  module Sistero
8
- Profile = Struct.new(:access_token, :ssh_keys, :ssh_options, :vm_name, :vm_size, :vm_region, :vm_image)
9
- # DEFAULTS = {
10
- # :access_token => nil,
11
- # :ssh_keys => [],
12
- # :ssh_options => '',
13
- # :vm_name => nil,
14
- # :vm_size => 512,
15
- # :vm_region => 'nyc3',
16
- # :vm_image => 'ubuntu-14-04-x64',
17
- # }
8
+ PROFILE_KEYS = [:vm_name, :vm_size, :vm_region, :vm_image, :access_token, :ssh_keys, :ssh_options]
9
+
10
+ Profile = Struct.new(*PROFILE_KEYS) do
11
+ def to_s
12
+ "vm #{vm_name}\n" + PROFILE_KEYS.map do |key|
13
+ val = self[key]
14
+ if val and key != :vm_name
15
+ " #{key} #{val}\n"
16
+ else
17
+ ""
18
+ end
19
+ end.join
20
+ end
21
+ end
18
22
 
19
23
  class Config
20
- attr_accessor :defaults
24
+ attr_accessor :defaults, :profiles
21
25
 
22
- def profile name
23
- @profiles[name] || @defaults
26
+ def profile vm_name
27
+ if @defaults['vm_name'] and vm_name.nil?
28
+ profile = @defaults
29
+ else
30
+ # TODO: also handle wildcards
31
+ profile = @profiles.find do |profile|
32
+ profile.vm_name == vm_name
33
+ end
34
+ raise "could not find profile for #{vm_name}" unless profile
35
+ end
36
+ profile
24
37
  end
25
38
 
26
39
  def initialize(opts = {})
27
40
  # read defaults from config file
28
41
  cfg_file_path = "#{ENV['HOME']}/.config/#{APP_NAME}"
29
42
  @defaults = Profile.new
30
- @profiles = {}
43
+ @profiles = []
31
44
 
32
45
  cfg = YAML.load_file cfg_file_path
33
46
  cfg['defaults'].each do |key, value|
34
47
  @defaults[key] = value
35
48
  end
36
49
 
37
- cfg.each do |name, profile_cfg|
38
- next if name == 'defaults'
39
- profile = @profiles[name] = Profile.new *@defaults
50
+ @profiles = cfg['profiles'].map do |profile_cfg|
51
+ profile = Profile.new *@defaults
40
52
  profile_cfg.each do |key, value|
41
53
  profile[key] = value
42
54
  end
55
+ profile
43
56
  end
57
+
58
+ @profiles.push(@defaults) if @defaults['vm_name']
59
+ end
60
+
61
+ def to_s
62
+ @profiles.map(&:to_s).join "\n"
44
63
  end
45
64
  end
46
65
 
@@ -50,7 +69,7 @@ module Sistero
50
69
  @client = DropletKit::Client.new(access_token: @config.defaults.access_token)
51
70
  end
52
71
 
53
- def find_vm(vm_name:)
72
+ def find_vm(vm_name)
54
73
  @client.droplets.all.find { |vm| vm.name == vm_name }
55
74
  end
56
75
 
@@ -60,10 +79,14 @@ module Sistero
60
79
  end
61
80
  end
62
81
 
63
- def create_vm(profile_name, vm_name: nil)
64
- profile = @config.profile profile_name
65
- vm_name ||= profile.vm_name
82
+ def get_profile vm_name
83
+ profile = @config.profile vm_name
84
+ vm_name = profile.vm_name if vm_name.nil?
85
+ [ profile, vm_name ]
86
+ end
66
87
 
88
+ def create_vm(vm_name)
89
+ profile, vm_name = get_profile vm_name
67
90
  puts "creating vm: #{vm_name}"
68
91
 
69
92
  vm = DropletKit::Droplet.new(
@@ -74,7 +97,7 @@ module Sistero
74
97
  ssh_keys: profile.ssh_keys
75
98
  )
76
99
  vm = @client.droplets.create(vm)
77
- puts "created vm: #{profile.to_s}"
100
+ puts "created: #{profile.to_s}"
78
101
  vm
79
102
  end
80
103
 
@@ -94,17 +117,16 @@ module Sistero
94
117
  return false
95
118
  end
96
119
 
97
- def ssh_to_vm(profile_name, vm_name: nil, ssh_options: nil)
98
- profile = @config.profile profile_name
99
- vm_name ||= profile.vm_name
120
+ def ssh_to_vm(vm_name, ssh_options: nil)
121
+ profile, vm_name = get_profile vm_name
100
122
  ssh_options ||= profile.ssh_options
101
123
 
102
- vm = find_vm(vm_name: vm_name) || create_vm(profile_name, vm_name: vm_name)
124
+ vm = find_vm(vm_name) || create_vm(vm_name)
103
125
  public_network = vm.networks.v4.find { |network| network.type == 'public' }
104
126
  until public_network
105
127
  puts "no public interfaces, trying again in a second"
106
128
  sleep 1
107
- vm = find_vm(vm_name: vm_name)
129
+ vm = find_vm(vm_name)
108
130
  public_network = vm.networks.v4.find { |network| network.type == 'public' }
109
131
  end
110
132
  ip = public_network.ip_address
@@ -122,11 +144,10 @@ module Sistero
122
144
  exec cmd
123
145
  end
124
146
 
125
- def destroy_vm(profile_name, vm_name: nil)
126
- profile = @config.profile profile_name
127
- vm_name ||= profile.vm_name
147
+ def destroy_vm(vm_name)
148
+ profile, vm_name = get_profile vm_name
128
149
 
129
- vm = find_vm(vm_name: vm_name)
150
+ vm = find_vm(vm_name)
130
151
  if vm
131
152
  puts "destroying #{vm.id}"
132
153
  @client.droplets.delete(id: vm.id)
@@ -134,5 +155,9 @@ module Sistero
134
155
  puts "vm #{vm_name} not found"
135
156
  end
136
157
  end
158
+
159
+ def show_config
160
+ puts @config.to_s
161
+ end
137
162
  end
138
163
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sistero
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Pike
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-31 00:00:00.000000000 Z
11
+ date: 2016-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler