yun 0.0.2 → 0.0.3

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.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'rspec/core/rake_task'
2
+ RSpec::Core::RakeTask.new('spec')
3
+
4
+ task :default => :spec
data/lib/yun.rb CHANGED
@@ -1,2 +1,2 @@
1
- require 'yun/yun'
2
- require 'yun/cli'
1
+ require 'yun/model'
2
+ require 'yun/commands/yun_command'
@@ -0,0 +1,20 @@
1
+ require 'thor'
2
+ require 'yun/model/connection'
3
+
4
+ module Yun
5
+ module CommandBase
6
+
7
+ def connection
8
+ @connection ||= Connection.new init_data
9
+ end
10
+
11
+ def init_data
12
+ {
13
+ :provider => "aws",
14
+ :aws_access_key_id => Config.aws_access_key_id,
15
+ :aws_secret_access_key => Config.aws_secret_access_key,
16
+ :region => Config.region
17
+ }
18
+ end
19
+ end
20
+ end
@@ -1,11 +1,50 @@
1
- require 'thor'
1
+ require 'hirb'
2
+ require 'yun/commands/command_base'
3
+ require 'yun/config'
2
4
 
3
5
  module Yun
4
- class Node < Thor
6
+ class NodeCommand < Thor
5
7
 
6
- desc "list", "list all nodes"
8
+ include CommandBase
9
+
10
+ desc "node list", "list all nodes"
7
11
  def list
8
- Yun.list
12
+ node_attributes = connection.list.map do |node|
13
+ {
14
+ :id => node.id,
15
+ :name => node.name,
16
+ :type => node.instance_type,
17
+ :image => node.image,
18
+ :ip => node.ip,
19
+ :created_at => node.created_at,
20
+ :state => node.state
21
+ }
22
+ end
23
+ puts Hirb::Helpers::AutoTable.render node_attributes
24
+ end
25
+
26
+ desc "node create NODE_NAME", "create a node"
27
+ method_option :image, :aliases => "-i", :default => "ami-11d68a54", :desc => "Amazon Machine Image"
28
+ method_option :instance_type, :aliases => "-t", :default => "t1.micro", :desc => "Instance Type"
29
+ def create(node_name)
30
+ attributes = create_attributes node_name, options
31
+ connection.create attributes
32
+ end
33
+
34
+ desc "node destroy NODE_NAME", "destroy a node"
35
+ def destroy node_name
36
+ node = connection.find node_name
37
+ node.destroy
38
+ end
39
+
40
+ private
41
+ def create_attributes node_name, options
42
+ {
43
+ "name" => node_name,
44
+ :image => options[:image],
45
+ :instance_type => options[:instance_type],
46
+ :key_name => Config.key_name
47
+ }
9
48
  end
10
49
 
11
50
  end
@@ -0,0 +1,26 @@
1
+ require 'thor'
2
+ require 'thor/group'
3
+ require 'yun/commands/command_base'
4
+ require 'yun/commands/node_command'
5
+
6
+ module Yun
7
+ class YunCommand < Thor
8
+
9
+ include CommandBase
10
+
11
+ desc "ssh NODE_NAME", "ssh to a node"
12
+ def ssh node_name
13
+ key = "~/.ssh/#{Config.key_name}.pem"
14
+ user_name = "ec2-user"
15
+
16
+ node = connection.find node_name
17
+ server_ip = node.ip
18
+
19
+ exec "ssh -i #{key} #{user_name}@#{server_ip}"
20
+ end
21
+
22
+ register NodeCommand, :node, "node SUBCOMMAND", "commands for node"
23
+ end
24
+ end
25
+
26
+ Yun::YunCommand.start
data/lib/yun/config.rb CHANGED
@@ -3,24 +3,29 @@ require 'yaml'
3
3
  module Yun
4
4
  class Config
5
5
 
6
- def initialize
7
- @setting = YAML.load_file(config_file)[:default]
6
+ def self.aws_access_key_id
7
+ setting[:aws_access_key_id]
8
8
  end
9
9
 
10
- def config_file
11
- @config_file ||= File.expand_path('~/.yun')
10
+ def self.aws_secret_access_key
11
+ setting[:aws_secret_access_key]
12
+ end
13
+
14
+ def self.region
15
+ setting[:region]
12
16
  end
13
17
 
14
- def aws_access_key_id
15
- @setting[:aws_access_key_id]
18
+ def self.key_name
19
+ setting[:key_name]
16
20
  end
17
21
 
18
- def aws_secret_access_key
19
- @setting[:aws_secret_access_key]
22
+ private
23
+ def self.setting
24
+ @setting ||= YAML.load_file(config_file)[:default]
20
25
  end
21
26
 
22
- def region
23
- @setting[:region]
27
+ def self.config_file
28
+ @config_file ||= File.expand_path('~/.yun')
24
29
  end
25
30
 
26
31
  end
data/lib/yun/model.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'fog'
2
+ require 'yun/model/connection'
3
+ require 'yun/model/fog_attributes'
4
+ require 'yun/model/node'
@@ -0,0 +1,29 @@
1
+ require 'yun/model'
2
+
3
+ module Yun
4
+ class Connection
5
+ def initialize options
6
+ @servers = Fog::Compute.new(options).servers
7
+ end
8
+
9
+ def create attributes={}
10
+ fog_attributes = FogAttributes.new attributes
11
+ server = @servers.create fog_attributes
12
+ server.wait_for { ready? }
13
+ Node.new server
14
+ end
15
+
16
+ def list
17
+ @servers.map do |server|
18
+ Node.new server
19
+ end
20
+ end
21
+
22
+ def find node_name
23
+ list.find do |node|
24
+ node.name == node_name
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,40 @@
1
+ module Yun
2
+ class FogAttributes < Hash
3
+
4
+ def initialize attributes
5
+ convert_basic_attributes attributes
6
+ convert_tags_attributes attributes
7
+ end
8
+
9
+ private
10
+ def convert_basic_attributes attributes
11
+ attributes.each do |key, value|
12
+ convert key, value
13
+ end
14
+ end
15
+
16
+ def convert key, value
17
+ if fog_key_mapping.has_key? key
18
+ fog_key = fog_key_mapping[key]
19
+ self[fog_key] = value
20
+ end
21
+ end
22
+
23
+ def fog_key_mapping
24
+ {
25
+ :image => :image_id,
26
+ :instance_type => :flavor_id,
27
+ :key_name => :key_name
28
+ }
29
+ end
30
+
31
+ def convert_tags_attributes attributes
32
+ tags_key = ["name"]
33
+ tags_hash = attributes.reject do |key, value|
34
+ not tags_key.include? key
35
+ end
36
+ self[:tags] = tags_hash
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,46 @@
1
+ module Yun
2
+ class Node
3
+
4
+ def initialize server
5
+ @server = server
6
+ end
7
+
8
+ def id
9
+ @server.id
10
+ end
11
+
12
+ def image
13
+ @server.image_id
14
+ end
15
+
16
+ def instance_type
17
+ @server.flavor_id
18
+ end
19
+
20
+ def key_name
21
+ @server.key_name
22
+ end
23
+
24
+ def name
25
+ @server.tags["name"]
26
+ end
27
+
28
+ def state
29
+ @server.state
30
+ end
31
+
32
+ def ip
33
+ @server.public_ip_address
34
+ end
35
+
36
+ def created_at
37
+ @server.created_at
38
+ end
39
+
40
+ def destroy
41
+ @server.destroy
42
+ @server.wait_for { not reload or state == 'terminated' }
43
+ end
44
+
45
+ end
46
+ end
data/lib/yun/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yun
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,10 @@
1
+ require 'rspec'
2
+ require 'fog'
3
+ require 'yun'
4
+
5
+ Fog.mock!
6
+
7
+ RSpec.configure do |config|
8
+ config.color_enabled = true
9
+ config.formatter = 'documentation'
10
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+
3
+ describe Yun::Connection do
4
+
5
+ config = {
6
+ :provider => 'aws',
7
+ :aws_access_key_id => 'fake_aws_access_key_id',
8
+ :aws_secret_access_key => 'fake_secret_access_key',
9
+ :region => 'us-west-1'
10
+ }
11
+
12
+ before :all do
13
+ Fog::Compute.new(config).key_pairs.create(:name => 'some_key')
14
+ end
15
+
16
+ after :each do
17
+ Fog::Compute.new(config).servers.each do |server|
18
+ server.destroy
19
+ end
20
+ end
21
+
22
+ context 'create' do
23
+
24
+ before :each do
25
+ @connection = Yun::Connection.new config
26
+ end
27
+
28
+ it 'should create server with default value when no attributes given' do
29
+ node = @connection.create
30
+
31
+ node.instance_type.should == 't1.micro'
32
+ end
33
+
34
+ it 'should make sure the server is running in creation' do
35
+ node = @connection.create
36
+
37
+ node.state.should == 'running'
38
+ end
39
+
40
+ it 'should create server using given attributes' do
41
+ attributes =
42
+ {
43
+ :image => 'test_image_id',
44
+ :instance_type => 'small_type',
45
+ :key_name => 'some_key'
46
+ }
47
+ node = @connection.create attributes
48
+
49
+ node.image.should == 'test_image_id'
50
+ node.instance_type.should == 'small_type'
51
+ node.key_name.should == 'some_key'
52
+ end
53
+
54
+ it 'should create server with name' do
55
+ attributes = { "name" => 'test server' }
56
+
57
+ node = @connection.create attributes
58
+
59
+ node.name.should == 'test server'
60
+ end
61
+
62
+ end
63
+
64
+ context 'list' do
65
+
66
+ before :each do
67
+ @connection = Yun::Connection.new config
68
+
69
+ @one_node = @connection.create({ "name" => 'one node' })
70
+ @other_node = @connection.create({ "name" => 'other node' })
71
+ end
72
+
73
+ it 'should return all the node' do
74
+ nodes = @connection.list
75
+
76
+ nodes.length.should == 2
77
+ names = nodes.map { |node| node.name }
78
+ names.sort.should == ['one node', 'other node']
79
+ end
80
+
81
+ end
82
+
83
+ context 'find' do
84
+ before do
85
+ @connection = Yun::Connection.new config
86
+ @one_node = @connection.create({ "name" => 'one node' })
87
+ end
88
+
89
+ it 'should find the node by node name' do
90
+ node = @connection.find 'one node'
91
+
92
+ node.name.should == 'one node'
93
+ end
94
+ end
95
+
96
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Yun::FogAttributes do
4
+
5
+ it 'should convert attributes to fog attributes hash' do
6
+ attribute = { :image => 'test_image_id' }
7
+
8
+ fog_attr = Yun::FogAttributes.new attribute
9
+
10
+ fog_attr[:image_id].should == 'test_image_id'
11
+ end
12
+
13
+ it 'should save name attribute into tags' do
14
+ attribute = { "name" => 'test name' }
15
+
16
+ fog_attr = Yun::FogAttributes.new attribute
17
+
18
+ fog_attr[:tags]["name"].should == 'test name'
19
+ end
20
+
21
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe Yun::Node do
4
+
5
+ options = {
6
+ :provider => 'aws',
7
+ :aws_access_key_id => 'fake_aws_access_key_id',
8
+ :aws_secret_access_key => 'fake_secret_access_key',
9
+ :region => 'us-west-1'
10
+ }
11
+
12
+ after :each do
13
+ Fog::Compute.new(options).servers.each do |server|
14
+ server.destroy
15
+ end
16
+ end
17
+
18
+ context 'basic attributes' do
19
+
20
+ before do
21
+ @time = Time.now
22
+ attributes =
23
+ {
24
+ :id => 'id',
25
+ :image_id => 'some_image',
26
+ :flavor_id => 'small_type',
27
+ :key_name => 'some_key',
28
+ :state => 'running',
29
+ :public_ip_address => '127.0.0.1',
30
+ :created_at => @time
31
+ }
32
+ @ec2_node = Fog::Compute::AWS::Server.new attributes
33
+ end
34
+
35
+ it 'should convert all the ec2 node attribute' do
36
+ node = Yun::Node.new @ec2_node
37
+
38
+ node.id.should == 'id'
39
+ node.image.should == 'some_image'
40
+ node.instance_type.should == 'small_type'
41
+ node.key_name.should == 'some_key'
42
+ node.state.should == 'running'
43
+ node.ip.should == '127.0.0.1'
44
+ node.created_at == @time
45
+ end
46
+
47
+ end
48
+
49
+ context 'node name' do
50
+
51
+ before do
52
+ attributes = { :tags => { "name" => 'test node' } }
53
+ @ec2_node = Fog::Compute::AWS::Server.new attributes
54
+ end
55
+
56
+ it 'should parse the name in the tags value' do
57
+ node = Yun::Node.new @ec2_node
58
+
59
+ node.name.should == 'test node'
60
+ end
61
+
62
+ end
63
+
64
+ context 'destroy' do
65
+
66
+ before do
67
+ @connection = Yun::Connection.new options
68
+ end
69
+
70
+ it 'should destroy ec2 node' do
71
+ node = @connection.create
72
+
73
+ node.destroy
74
+
75
+ @connection.list.length.should == 0
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+ require 'yun/version'
3
+
4
+ describe Yun do
5
+ it 'should return the version' do
6
+ Yun::VERSION.should == '0.0.2'
7
+ end
8
+ end
data/yun.gemspec CHANGED
@@ -17,6 +17,8 @@ Gem::Specification.new do |gem|
17
17
  gem.executables = "yun"
18
18
 
19
19
  gem.add_runtime_dependency "fog", ">= 1.0.0"
20
- gem.add_runtime_dependency 'hirb', '~> 0.4.5'
20
+ gem.add_runtime_dependency 'hirb', '>= 0.4.5'
21
21
  gem.add_runtime_dependency 'thor', '>= 0.14.0'
22
+
23
+ gem.add_development_dependency 'rspec', '~> 2.7'
22
24
  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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
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-11-20 00:00:00 Z
18
+ date: 2011-12-09 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: fog
@@ -39,7 +39,7 @@ dependencies:
39
39
  requirement: &id002 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
- - - ~>
42
+ - - ">="
43
43
  - !ruby/object:Gem::Version
44
44
  hash: 5
45
45
  segments:
@@ -65,6 +65,21 @@ dependencies:
65
65
  version: 0.14.0
66
66
  type: :runtime
67
67
  version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: rspec
70
+ prerelease: false
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ hash: 13
77
+ segments:
78
+ - 2
79
+ - 7
80
+ version: "2.7"
81
+ type: :development
82
+ version_requirements: *id004
68
83
  description:
69
84
  email:
70
85
  - flankerfc@gmail.com
@@ -75,16 +90,27 @@ extensions: []
75
90
  extra_rdoc_files: []
76
91
 
77
92
  files:
93
+ - .gitignore
78
94
  - .rvmrc
79
95
  - Gemfile
80
96
  - Gemfile.lock
97
+ - Rakefile
81
98
  - bin/yun
82
99
  - lib/yun.rb
83
- - lib/yun/cli.rb
100
+ - lib/yun/commands/command_base.rb
84
101
  - lib/yun/commands/node_command.rb
102
+ - lib/yun/commands/yun_command.rb
85
103
  - lib/yun/config.rb
104
+ - lib/yun/model.rb
105
+ - lib/yun/model/connection.rb
106
+ - lib/yun/model/fog_attributes.rb
107
+ - lib/yun/model/node.rb
86
108
  - lib/yun/version.rb
87
- - lib/yun/yun.rb
109
+ - spec/spec_helper.rb
110
+ - spec/yun/model/connection_spec.rb
111
+ - spec/yun/model/fog_attributes_spec.rb
112
+ - spec/yun/model/node_spec.rb
113
+ - spec/yun/version_spec.rb
88
114
  - yun.gemspec
89
115
  homepage: https://github.com/flanker/yun
90
116
  licenses: []
data/lib/yun/cli.rb DELETED
@@ -1,12 +0,0 @@
1
- require 'thor'
2
- require 'thor/group'
3
- require 'yun/commands/node_command'
4
-
5
- module Yun
6
- class CLI < Thor
7
-
8
- register Node, :node, "node", "commands for node"
9
- end
10
- end
11
-
12
- Yun::CLI.start
data/lib/yun/yun.rb DELETED
@@ -1,32 +0,0 @@
1
- require 'fog'
2
- require 'hirb'
3
- require 'yun/config'
4
-
5
- module Yun
6
- class Yun
7
-
8
- def self.list
9
- config = Config.new
10
- options = {:provider => "aws",
11
- :aws_access_key_id => config.aws_access_key_id,
12
- :aws_secret_access_key => config.aws_secret_access_key,
13
- :region => config.region}
14
-
15
- nodes = Fog::Compute.new(options).servers.map do |node|
16
- {
17
- :id => node.id,
18
- :flavor_id => node.flavor_id,
19
- :image_id => node.image_id,
20
- :address => node.private_ip_address,
21
- :created_at => node.created_at,
22
- :status => node.state,
23
- :instance_type => node.root_device_type
24
- }
25
- end.sort_by do |node|
26
- node[:id] || ""
27
- end
28
-
29
- puts Hirb::Helpers::AutoTable.render nodes
30
- end
31
- end
32
- end