some 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/TODO +2 -0
  2. data/VERSION +1 -1
  3. data/bin/some +5 -52
  4. data/lib/some.rb +27 -29
  5. metadata +8 -6
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ * インデント直す
2
+ * ポート開ける用の cookbooks 作る
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
data/bin/some CHANGED
@@ -73,65 +73,18 @@ class CLI < Thor
73
73
  end
74
74
  end
75
75
 
76
- desc "console [<instance_id or hostname>]", "get console output for instance or first available"
77
- def console(id=nil)
78
- inst = some.find(id) || (some.running | some.pending).first || abort("No running or pending instances")
79
-
80
- puts some.console_output(inst[:instance_id]).inspect
81
- end
82
-
83
76
  desc "terminate [<instance_id or hostname>]", "terminate specified instance or first available"
84
77
  def terminate(id=nil)
85
78
  inst = some.find(id) || (some.running | some.pending).first || abort("No running or pending instances")
86
-
79
+ if inst[:status] != 'stopped'
80
+ task 'Wait to stop' do
81
+ some.wait_to_stop(inst[:instance_id])
82
+ end
83
+ end
87
84
  some.terminate(inst[:instance_id])
88
85
  puts "#{inst[:hostname] || inst[:instance_id]} scheduled for termination"
89
86
  end
90
87
 
91
- desc "terminate_all", "terminate all instances"
92
- def terminate_all
93
- instances = (some.running | some.pending)
94
- abort("No running or pending instances") if instances.empty?
95
- instances.each do |inst|
96
- some.terminate(inst[:instance_id])
97
- puts "#{inst[:hostname] || inst[:instance_id]} scheduled for termination"
98
- end
99
- end
100
-
101
- desc "volumes", "list all volumes"
102
- def volumes
103
- some.volumes.each do |v|
104
- printf "%-10s %4sGB %10s %15s %15s\n", v[:volume_id], v[:size], v[:status], v[:instance], v[:device]
105
- end
106
- end
107
-
108
- desc "create_volume [<megabytes>]", "create a volume"
109
- def create_volume(size=5)
110
- task("Create #{size}GB volume") { some.create_volume(size) }
111
- end
112
-
113
- desc "destroy_volume [<volume_id>]", "destroy a volume"
114
- def destroy_volume(volume=nil)
115
- vol_id = (some.find_volume(volume) || some.nondestroyed_volumes.first || abort("No volumes"))[:volume_id]
116
- task("Destroy volume") { some.destroy_volume(vol_id) }
117
- end
118
-
119
- desc "attach [<volume_id>] [<instance_id or hostname>] [<device>]", "attach volume to running instance"
120
- def attach(volume=nil, inst_id=nil, device=nil)
121
- vol_id = (some.find_volume(volume) || some.available_volumes.first || abort("No available volumes"))[:volume_id]
122
- inst_id = (some.find(inst_id) || some.running.first || abort("No running instances"))[:instance_id]
123
- device ||= '/dev/sdc1'
124
- task("Attach #{vol_id} to #{inst_id} as #{device}") do
125
- some.attach(vol_id, inst_id, device)
126
- end
127
- end
128
-
129
- desc "detach [<volume_id>]", "detach volume from instance"
130
- def detach(volume=nil)
131
- vol_id = (some.find_volume(volume) || some.attached_volumes.first || abort("No attached volumes"))[:volume_id]
132
- task("Detach #{vol_id}") { some.detach(vol_id) }
133
- end
134
-
135
88
  no_tasks do
136
89
  def some
137
90
  @some ||= Some.new
data/lib/some.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'net/ssh'
1
2
  require 'NIFTY'
2
3
  require 'yaml'
3
4
  require 'socket'
@@ -146,6 +147,21 @@ class Some
146
147
  end
147
148
  end
148
149
 
150
+ def wait_to_stop(instance_id)
151
+ raise ArgumentError unless instance_id
152
+ api.stop_instances(:instance_id => [ instance_id ])
153
+ puts "waiting for #{instance_id} to stop "
154
+ loop do
155
+ print '.'
156
+ if inst = instance_info(instance_id)
157
+ if inst[:status] == 'stopped'
158
+ break
159
+ end
160
+ end
161
+ sleep 5
162
+ end
163
+ end
164
+
149
165
  def wait_for_ssh(hostname)
150
166
  raise ArgumentError unless hostname
151
167
  loop do
@@ -177,12 +193,12 @@ class Some
177
193
  end
178
194
 
179
195
  def ssh(hostname, cmds)
180
- IO.popen("ssh -i #{keypair_file} #{config['user']}@#{hostname} > ~/.some/ssh.log 2>&1", "w") do |pipe|
181
- pipe.puts cmds.join(' && ')
182
- end
183
- unless $?.success?
184
- abort "failed\nCheck ~/.some/ssh.log for the output"
185
- end
196
+ Net::SSH.start(hostname, config['user'], :keys => [keypair_file], :passphrase => config['password']) do |ssh|
197
+ File.open("#{ENV['HOME']}/.some/ssh.log", 'w') do |f|
198
+ f.write(ssh.exec!(cmds.join(' && ')))
199
+ end
200
+ end
201
+ # TODO: abort "failed\nCheck ~/.some/ssh.log for the output"
186
202
  end
187
203
 
188
204
  def resources(hostname)
@@ -204,27 +220,9 @@ class Some
204
220
  end
205
221
 
206
222
  def terminate(instance_id)
207
- inst = instance_info(instance_id)
208
- if inst[:status] != 'stopped'
209
- api.stop_instances(:instance_id => [ instance_id ])
210
- puts "waiting for #{instance_id} to stop "
211
- loop do
212
- print '.'
213
- if inst = instance_info(instance_id)
214
- if inst[:status] == 'stopped'
215
- break
216
- end
217
- end
218
- sleep 5
219
- end
220
- end
221
223
  api.terminate_instances(:instance_id => [ instance_id ])
222
224
  end
223
225
 
224
- def console_output(instance_id)
225
- api.get_console_output(:instance_id => instance_id)["output"]
226
- end
227
-
228
226
  def config
229
227
  @config ||= default_config.merge read_config
230
228
  end
@@ -232,7 +230,7 @@ class Some
232
230
  def default_config
233
231
  {
234
232
  'user' => 'root',
235
- 'ami' => 'ami-ed46a784',
233
+ 'ami' => 26,
236
234
  'availability_zone' => 'east-12',
237
235
  'password' => 'password'
238
236
  }
@@ -282,10 +280,10 @@ class Some
282
280
 
283
281
  def api
284
282
  @api ||= NIFTY::Cloud::Base.new(
285
- :access_key => config['access_key'],
286
- :secret_key => config['secret_key'],
287
- :server => server,
288
- :path => '/api'
283
+ :access_key => config['access_key'],
284
+ :secret_key => config['secret_key'],
285
+ :server => server,
286
+ :path => '/api'
289
287
  )
290
288
  end
291
289
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: some
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-04 00:00:00.000000000Z
12
+ date: 2013-04-05 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nifty-cloud-sdk
16
- requirement: &110762780 !ruby/object:Gem::Requirement
16
+ requirement: &259249220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *110762780
24
+ version_requirements: *259249220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: thor
27
- requirement: &110762200 !ruby/object:Gem::Requirement
27
+ requirement: &259248640 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *110762200
35
+ version_requirements: *259248640
36
36
  description: sumo clone for NIFTY Cloud
37
37
  email: tidnlyam@gmail.com
38
38
  executables:
@@ -40,9 +40,11 @@ executables:
40
40
  extensions: []
41
41
  extra_rdoc_files:
42
42
  - README.rdoc
43
+ - TODO
43
44
  files:
44
45
  - README.rdoc
45
46
  - Rakefile
47
+ - TODO
46
48
  - VERSION
47
49
  - bin/some
48
50
  - lib/some.rb