yun 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  require 'hirb'
2
+ require 'pp'
2
3
 
3
4
  module Yun
4
5
  class NodeCommand < Thor
@@ -12,7 +13,7 @@ module Yun
12
13
  :id => node.id,
13
14
  :name => node.name,
14
15
  :type => node.instance_type,
15
- :image => node.image,
16
+ :os => node.os,
16
17
  :ip => node.ip,
17
18
  :created_at => node.created_at.strftime("%Y-%m-%d %H:%M:%S"),
18
19
  :state => node.state
@@ -22,25 +23,43 @@ module Yun
22
23
  end
23
24
 
24
25
  desc "node create NODE_NAME", "create a node"
25
- method_option :image, :aliases => "-i", :default => "ami-2e10406b", :desc => "Amazon Machine Image"
26
- method_option :instance_type, :aliases => "-t", :default => "t1.micro", :desc => "Instance Type"
26
+ method_option :os, :aliases => "-o", :default => "ubuntu", :desc => "OS Name"
27
+ method_option :instance_type, :aliases => "-t", :default => "micro", :desc => "Instance Type"
27
28
  def create(node_name)
29
+ $stdout.sync = true
28
30
  attributes = create_attributes node_name, options
29
- connection.create attributes
31
+ print "creating node."
32
+ connection.create attributes do
33
+ print "."
34
+ end
35
+ puts "\ndone"
30
36
  end
31
37
 
32
38
  desc "node destroy NODE_NAME", "destroy a node"
33
39
  def destroy node_name
40
+ $stdout.sync = true
41
+ node = connection.find node_name
42
+ print "destroy node."
43
+ node.destroy { print "." }
44
+ puts "\ndone"
45
+ end
46
+
47
+ desc "node info NODE_NAME", "show a node's info"
48
+ def info node_name
49
+ $stdout.sync = true
34
50
  node = connection.find node_name
35
- node.destroy
51
+ pp node.all_info
36
52
  end
37
53
 
38
54
  private
39
55
  def create_attributes node_name, options
56
+ os = options[:os]
40
57
  {
41
58
  "name" => node_name,
42
- :image => options[:image],
43
- :instance_type => options[:instance_type],
59
+ "os" => os,
60
+ "user" => Config.get_user(os),
61
+ :image => Config.get_image(os),
62
+ :instance_type => InstanceType.parse(options[:instance_type]),
44
63
  :key_name => Config.key_name
45
64
  }
46
65
  end
@@ -5,8 +5,8 @@ module Yun
5
5
 
6
6
  desc "ssh NODE_NAME", "ssh to a node"
7
7
  def ssh node_name
8
- ssh_config = SshConfig.new Config.key_name
9
8
  node = connection.find node_name
9
+ ssh_config = SshConfig.new node.user, node.key_name
10
10
  ssh = Ssh.new node.ip, ssh_config
11
11
 
12
12
  ssh.connect
@@ -14,8 +14,8 @@ module Yun
14
14
 
15
15
  desc "test NODE_NAME, ROLE", "provision node with chef"
16
16
  def chef node_name, role
17
- ssh_config = SshConfig.new Config.key_name
18
17
  node = connection.find node_name
18
+ ssh_config = SshConfig.new node.user, node.key_name
19
19
  ssh = Ssh.new node.ip, ssh_config
20
20
 
21
21
  ssh.chef role
@@ -23,6 +23,14 @@ module Yun
23
23
  setting[:chef_repo]
24
24
  end
25
25
 
26
+ def self.get_image os_name
27
+ setting[:os][os_name.to_sym][:image]
28
+ end
29
+
30
+ def self.get_user os_name
31
+ setting[:os][os_name.to_sym][:user]
32
+ end
33
+
26
34
  private
27
35
  def self.setting
28
36
  @setting ||= YAML.load_file(config_file)[:default]
@@ -1,4 +1,5 @@
1
1
  require 'fog'
2
+ require 'yun/model/instance_type'
2
3
  require 'yun/model/connection'
3
4
  require 'yun/model/fog_attributes'
4
5
  require 'yun/model/node'
@@ -6,10 +6,13 @@ module Yun
6
6
  @servers = Fog::Compute.new(options).servers
7
7
  end
8
8
 
9
- def create attributes={}
9
+ def create attributes={}, &block
10
10
  fog_attributes = FogAttributes.new attributes
11
11
  server = @servers.create fog_attributes
12
- server.wait_for { ready? }
12
+ server.wait_for do
13
+ instance_eval(&block) if block_given?
14
+ ready?
15
+ end
13
16
  Node.new server
14
17
  end
15
18
 
@@ -20,9 +23,14 @@ module Yun
20
23
  end
21
24
 
22
25
  def find node_name
23
- list.find do |node|
24
- node.name == node_name
26
+ node = list.find do |node|
27
+ node.name == node_name and not node.is_destroyed?
28
+ end
29
+ if node == nil
30
+ puts "Error: Cannot find node #{node_name}"
31
+ exit 1
25
32
  end
33
+ node
26
34
  end
27
35
 
28
36
  end
@@ -29,7 +29,7 @@ module Yun
29
29
  end
30
30
 
31
31
  def convert_tags_attributes attributes
32
- tags_key = ["name"]
32
+ tags_key = ["name", "os", "user"]
33
33
  tags_hash = attributes.reject do |key, value|
34
34
  not tags_key.include? key
35
35
  end
@@ -0,0 +1,10 @@
1
+ module Yun
2
+ class InstanceType
3
+ def self.parse readable_type
4
+ case readable_type
5
+ when "micro" then "t1.micro"
6
+ when "small" then "m1.small"
7
+ end
8
+ end
9
+ end
10
+ end
@@ -25,6 +25,14 @@ module Yun
25
25
  @server.tags["name"]
26
26
  end
27
27
 
28
+ def os
29
+ @server.tags["os"]
30
+ end
31
+
32
+ def user
33
+ @server.tags["user"]
34
+ end
35
+
28
36
  def state
29
37
  @server.state
30
38
  end
@@ -37,9 +45,20 @@ module Yun
37
45
  @server.created_at
38
46
  end
39
47
 
40
- def destroy
48
+ def destroy &block
41
49
  @server.destroy
42
- @server.wait_for { not reload or state == 'terminated' }
50
+ @server.wait_for do
51
+ instance_eval(&block) if block_given?
52
+ not reload or state == 'terminated'
53
+ end
54
+ end
55
+
56
+ def all_info
57
+ @server
58
+ end
59
+
60
+ def is_destroyed?
61
+ @server.state == 'terminated'
43
62
  end
44
63
 
45
64
  end
@@ -10,14 +10,13 @@ module Yun
10
10
  end
11
11
 
12
12
  def connect
13
- exec "ssh -i #{key_file} #{user}@#{@host}"
13
+ ssh_command = "ssh -i #{key_file} #{user}@#{@host}"
14
+ puts ssh_command
15
+ exec ssh_command
14
16
  end
15
17
 
16
18
  def chef role
17
19
  Net::SSH.start(@host, user, :keys => [key_file]) do |ssh|
18
- puts "installing chef"
19
- remote_command ssh, "bash /tmp/install_chef_file.sh"
20
-
21
20
  puts "packaging chef repo"
22
21
  tmp_chef_repo_tar = make_chef_repo_tar Config.chef_repo
23
22
 
@@ -25,6 +24,10 @@ module Yun
25
24
  ssh.scp.upload! tmp_chef_repo_tar, tmp_chef_repo_tar
26
25
  ssh.scp.upload! install_chef_file, "/tmp/install_chef_file.sh"
27
26
  ssh.scp.upload! chef_config_file, "/tmp/chef-solo.rb"
27
+
28
+ puts "installing chef"
29
+ remote_command ssh, "bash /tmp/install_chef_file.sh"
30
+
28
31
  remote_command ssh, "echo {\\\"run_list\\\":\\\"role[#{role}]\\\"} > /tmp/node.json"
29
32
 
30
33
  puts "executing chef"
@@ -1,17 +1,18 @@
1
1
  module Yun
2
2
  class SshConfig
3
3
 
4
- def initialize key_name
4
+ def initialize user, key_name
5
+ @user = user
5
6
  @key_name = key_name
6
7
  end
7
8
 
8
9
  def user
9
- "bitnami"
10
+ @user
10
11
  end
11
12
 
12
13
  def key_file
13
14
  "~/.ssh/#{@key_name}.pem"
14
15
  end
15
-
16
+
16
17
  end
17
18
  end
@@ -1,3 +1,3 @@
1
1
  module Yun
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe Yun::InstanceType do
4
+
5
+ it 'should parse the human readable name to ec2 instance type' do
6
+ Yun::InstanceType.parse("micro").should == "t1.micro"
7
+ Yun::InstanceType.parse("small").should == "m1.small"
8
+ end
9
+ end
10
+
@@ -3,13 +3,14 @@ require 'spec_helper'
3
3
  describe Yun::SshConfig do
4
4
 
5
5
  before do
6
+ @user = 'some_user'
6
7
  @key_name = 'some_key'
7
8
  end
8
9
 
9
10
  it 'should construct from config object' do
10
- ssh_config = Yun::SshConfig.new @key_name
11
+ ssh_config = Yun::SshConfig.new @user, @key_name
11
12
 
12
- ssh_config.user.should == 'ec2-user'
13
+ ssh_config.user.should == @user
13
14
  ssh_config.key_file.should == '~/.ssh/some_key.pem'
14
15
  end
15
16
 
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Yun::Ssh do
4
4
 
5
5
  before do
6
- @ssh_config = Yun::SshConfig.new "test_key"
6
+ @ssh_config = Yun::SshConfig.new "user", "test_key"
7
7
  end
8
8
 
9
9
  it 'should execute the correct ssh command' do
@@ -3,6 +3,6 @@ require 'yun/version'
3
3
 
4
4
  describe Yun do
5
5
  it 'should return the version' do
6
- Yun::VERSION.should == '0.0.4'
6
+ Yun::VERSION.should == '0.0.5'
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yun
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Feng Zhichao
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-12-10 00:00:00 Z
18
+ date: 2012-01-08 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: fog
@@ -106,6 +106,7 @@ files:
106
106
  - lib/yun/model.rb
107
107
  - lib/yun/model/connection.rb
108
108
  - lib/yun/model/fog_attributes.rb
109
+ - lib/yun/model/instance_type.rb
109
110
  - lib/yun/model/node.rb
110
111
  - lib/yun/model/ssh.rb
111
112
  - lib/yun/model/ssh_config.rb
@@ -115,6 +116,7 @@ files:
115
116
  - spec/spec_helper.rb
116
117
  - spec/yun/model/connection_spec.rb
117
118
  - spec/yun/model/fog_attributes_spec.rb
119
+ - spec/yun/model/instance_type_spec.rb
118
120
  - spec/yun/model/node_spec.rb
119
121
  - spec/yun/model/ssh_config_spec.rb
120
122
  - spec/yun/model/ssh_spec.rb