yun 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  *.gem
2
+ tags
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ yun 云
2
+ ======
3
+
4
+ About
5
+ -----
6
+
7
+ `yun` is a command line tool for manage Amazon EC2 environment.
8
+
9
+ It's still working in process. Now it supports `create`, `destroy`,
10
+ `list`, `ssh`, `chef provision` commands.
11
+
12
+ Ruby Gem
13
+ --------
14
+
15
+ Install `yun` by
16
+
17
+ (sudo) gem install yun
18
+
19
+ You can find `yun` in rubygems.org
20
+
21
+ https://rubygems.org/gems/yun
22
+
23
+ How to use
24
+ ----------
25
+
26
+ ### config file
27
+ After installing `yun`, you need to create a config file `~/.yun`.
28
+ Basically it should contains:
29
+
30
+ :default:
31
+ :aws_access_key_id: YOUR_AWS_ACCESS_KEY_ID
32
+ :aws_secret_access_key: YOUR_AWS_SECRET_ACCESS_KEY
33
+ :region: us-west-1
34
+ :key_name: YOUR_KEY_PAIR_NAME
35
+ :chef_repo: /PATH/TO/YOUR/CHEF_REPO
36
+
37
+ ### create EC2 node
38
+
39
+ yun node create NODE_NAME
40
+
41
+ ### list all EC2 node
42
+
43
+ yun node list
44
+
45
+ ### destroy EC2 node
46
+
47
+ yun node destroy NODE_NAME
48
+
49
+ ### ssh to EC2 node
50
+
51
+ yun ssh NODE_NAME
52
+
53
+ ### provision EC2 node using chef
54
+
55
+ yun chef NODE_NAME ROLE_NAME
56
+
57
+ Other
58
+ -----
59
+
60
+ Feel free to contact me if you have any problem:
61
+
62
+ * flankerfc at Gmail
63
+ * @冯智超 at Sina Weibo
@@ -0,0 +1,5 @@
1
+ require 'thor'
2
+ require 'thor/group'
3
+ require 'yun/commands/command_base'
4
+ require 'yun/commands/node_command'
5
+ require 'yun/commands/yun_command'
@@ -1,6 +1,4 @@
1
1
  require 'hirb'
2
- require 'yun/commands/command_base'
3
- require 'yun/config'
4
2
 
5
3
  module Yun
6
4
  class NodeCommand < Thor
@@ -16,7 +14,7 @@ module Yun
16
14
  :type => node.instance_type,
17
15
  :image => node.image,
18
16
  :ip => node.ip,
19
- :created_at => node.created_at,
17
+ :created_at => node.created_at.strftime("%Y-%m-%d %H:%M:%S"),
20
18
  :state => node.state
21
19
  }
22
20
  end
@@ -24,7 +22,7 @@ module Yun
24
22
  end
25
23
 
26
24
  desc "node create NODE_NAME", "create a node"
27
- method_option :image, :aliases => "-i", :default => "ami-11d68a54", :desc => "Amazon Machine Image"
25
+ method_option :image, :aliases => "-i", :default => "ami-2e10406b", :desc => "Amazon Machine Image"
28
26
  method_option :instance_type, :aliases => "-t", :default => "t1.micro", :desc => "Instance Type"
29
27
  def create(node_name)
30
28
  attributes = create_attributes node_name, options
@@ -1,8 +1,3 @@
1
- require 'thor'
2
- require 'thor/group'
3
- require 'yun/commands/command_base'
4
- require 'yun/commands/node_command'
5
-
6
1
  module Yun
7
2
  class YunCommand < Thor
8
3
 
@@ -10,16 +5,25 @@ module Yun
10
5
 
11
6
  desc "ssh NODE_NAME", "ssh to a node"
12
7
  def ssh node_name
13
- key = "~/.ssh/#{Config.key_name}.pem"
14
- user_name = "ec2-user"
8
+ ssh_config = SshConfig.new Config.key_name
9
+ node = connection.find node_name
10
+ ssh = Ssh.new node.ip, ssh_config
15
11
 
12
+ ssh.connect
13
+ end
14
+
15
+ desc "test NODE_NAME, ROLE", "provision node with chef"
16
+ def chef node_name, role
17
+ ssh_config = SshConfig.new Config.key_name
16
18
  node = connection.find node_name
17
- server_ip = node.ip
19
+ ssh = Ssh.new node.ip, ssh_config
18
20
 
19
- exec "ssh -i #{key} #{user_name}@#{server_ip}"
21
+ ssh.chef role
20
22
  end
21
23
 
22
24
  register NodeCommand, :node, "node SUBCOMMAND", "commands for node"
25
+
26
+ private
23
27
  end
24
28
  end
25
29
 
data/lib/yun/config.rb CHANGED
@@ -19,6 +19,10 @@ module Yun
19
19
  setting[:key_name]
20
20
  end
21
21
 
22
+ def self.chef_repo
23
+ setting[:chef_repo]
24
+ end
25
+
22
26
  private
23
27
  def self.setting
24
28
  @setting ||= YAML.load_file(config_file)[:default]
@@ -0,0 +1,79 @@
1
+ require 'net/ssh'
2
+ require 'net/scp'
3
+
4
+ module Yun
5
+ class Ssh
6
+
7
+ def initialize host, ssh_config
8
+ @host = host
9
+ @ssh_config = ssh_config
10
+ end
11
+
12
+ def connect
13
+ exec "ssh -i #{key_file} #{user}@#{@host}"
14
+ end
15
+
16
+ def chef role
17
+ 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
+ puts "packaging chef repo"
22
+ tmp_chef_repo_tar = make_chef_repo_tar Config.chef_repo
23
+
24
+ puts "uploading chef repo"
25
+ ssh.scp.upload! tmp_chef_repo_tar, tmp_chef_repo_tar
26
+ ssh.scp.upload! install_chef_file, "/tmp/install_chef_file.sh"
27
+ ssh.scp.upload! chef_config_file, "/tmp/chef-solo.rb"
28
+ remote_command ssh, "echo {\\\"run_list\\\":\\\"role[#{role}]\\\"} > /tmp/node.json"
29
+
30
+ puts "executing chef"
31
+ remote_command ssh, "sudo chef-solo -c /tmp/chef-solo.rb -j /tmp/node.json -r /tmp/chef-solo.tar.gz"
32
+
33
+ puts "**********"
34
+ puts "DONE"
35
+ end
36
+ end
37
+
38
+ private
39
+ def user
40
+ @ssh_config.user
41
+ end
42
+
43
+ def key_file
44
+ @ssh_config.key_file
45
+ end
46
+
47
+ def make_chef_repo_tar chef_repo
48
+ tmp_tar = "/tmp/chef-solo.tar.gz"
49
+ Dir.chdir(chef_repo) do
50
+ system "tar -czvf #{tmp_tar} ./cookbooks ./roles"
51
+ end
52
+ tmp_tar
53
+ end
54
+
55
+ def install_chef_file
56
+ File.expand_path(File.dirname(__FILE__) + '/../templates/install_chef.sh')
57
+ end
58
+
59
+ def chef_config_file
60
+ File.expand_path(File.dirname(__FILE__) + '/../templates/chef-solo.rb')
61
+ end
62
+
63
+ def remote_command(ssh, command)
64
+ ssh.open_channel do |channel|
65
+ channel.exec(command) do |ch, success|
66
+ raise "command failed: #{command}" unless success
67
+
68
+ channel.on_data do |_ch, data|
69
+ $stdout.print data
70
+ end
71
+
72
+ channel.on_extended_data do |_ch, _type, data|
73
+ $stderr.print data
74
+ end
75
+ end
76
+ end.wait
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,17 @@
1
+ module Yun
2
+ class SshConfig
3
+
4
+ def initialize key_name
5
+ @key_name = key_name
6
+ end
7
+
8
+ def user
9
+ "bitnami"
10
+ end
11
+
12
+ def key_file
13
+ "~/.ssh/#{@key_name}.pem"
14
+ end
15
+
16
+ end
17
+ end
data/lib/yun/model.rb CHANGED
@@ -2,3 +2,5 @@ require 'fog'
2
2
  require 'yun/model/connection'
3
3
  require 'yun/model/fog_attributes'
4
4
  require 'yun/model/node'
5
+ require 'yun/model/ssh'
6
+ require 'yun/model/ssh_config'
@@ -0,0 +1,3 @@
1
+ file_cache_path "/var/chef-solo"
2
+ cookbook_path "/var/chef-solo/cookbooks"
3
+ role_path "/var/chef-solo/roles"
@@ -0,0 +1,14 @@
1
+ #!/bin/bash
2
+
3
+ sudo apt-get update
4
+ sudo apt-get install -y --force-yes ruby ruby-dev libopenssl-ruby rdoc ri irb build-essential wget ssl-cert git-core
5
+
6
+ if test ! -x /usr/bin/gem; then
7
+ cd /tmp
8
+ wget http://production.cf.rubygems.org/rubygems/rubygems-1.8.10.tgz
9
+ tar zxf rubygems-1.8.10.tgz
10
+ cd rubygems-1.8.10
11
+ sudo ruby setup.rb --no-format-executable
12
+ fi
13
+
14
+ sudo gem install chef --no-ri --no-rdoc
data/lib/yun/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yun
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/yun.rb CHANGED
@@ -1,2 +1,3 @@
1
+ require 'yun/config'
2
+ require 'yun/command'
1
3
  require 'yun/model'
2
- require 'yun/commands/yun_command'
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Yun::SshConfig do
4
+
5
+ before do
6
+ @key_name = 'some_key'
7
+ end
8
+
9
+ it 'should construct from config object' do
10
+ ssh_config = Yun::SshConfig.new @key_name
11
+
12
+ ssh_config.user.should == 'ec2-user'
13
+ ssh_config.key_file.should == '~/.ssh/some_key.pem'
14
+ end
15
+
16
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Yun::Ssh do
4
+
5
+ before do
6
+ @ssh_config = Yun::SshConfig.new "test_key"
7
+ end
8
+
9
+ it 'should execute the correct ssh command' do
10
+ ssh = Yun::Ssh.new "127.0.0.1", @ssh_config
11
+
12
+ # ssh.connect
13
+ end
14
+ end
@@ -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.2'
6
+ Yun::VERSION.should == '0.0.4'
7
7
  end
8
8
  end
data/yun.gemspec CHANGED
@@ -21,4 +21,15 @@ Gem::Specification.new do |gem|
21
21
  gem.add_runtime_dependency 'thor', '>= 0.14.0'
22
22
 
23
23
  gem.add_development_dependency 'rspec', '~> 2.7'
24
+
25
+ gem.post_install_message = <<-MSG
26
+ Thanks for installing yun!
27
+ You need put a config file in your home directory: ~/.yun
28
+ :default:
29
+ :aws_access_key_id: YOUR_AWS_ACCESS_KEY_ID
30
+ :aws_secret_access_key: YOUR_AWS_SECRET_ACCESS_KEY
31
+ :region: us-west-1
32
+ :key_name: YOUR_KEY_PAIR_NAME
33
+ :chef_repo: /PATH/TO/YOUR/CHEF_REPO
34
+ MSG
24
35
  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: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
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-09 00:00:00 Z
18
+ date: 2011-12-10 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: fog
@@ -94,9 +94,11 @@ files:
94
94
  - .rvmrc
95
95
  - Gemfile
96
96
  - Gemfile.lock
97
+ - README.md
97
98
  - Rakefile
98
99
  - bin/yun
99
100
  - lib/yun.rb
101
+ - lib/yun/command.rb
100
102
  - lib/yun/commands/command_base.rb
101
103
  - lib/yun/commands/node_command.rb
102
104
  - lib/yun/commands/yun_command.rb
@@ -105,17 +107,32 @@ files:
105
107
  - lib/yun/model/connection.rb
106
108
  - lib/yun/model/fog_attributes.rb
107
109
  - lib/yun/model/node.rb
110
+ - lib/yun/model/ssh.rb
111
+ - lib/yun/model/ssh_config.rb
112
+ - lib/yun/templates/chef-solo.rb
113
+ - lib/yun/templates/install_chef.sh
108
114
  - lib/yun/version.rb
109
115
  - spec/spec_helper.rb
110
116
  - spec/yun/model/connection_spec.rb
111
117
  - spec/yun/model/fog_attributes_spec.rb
112
118
  - spec/yun/model/node_spec.rb
119
+ - spec/yun/model/ssh_config_spec.rb
120
+ - spec/yun/model/ssh_spec.rb
113
121
  - spec/yun/version_spec.rb
114
122
  - yun.gemspec
115
123
  homepage: https://github.com/flanker/yun
116
124
  licenses: []
117
125
 
118
- post_install_message:
126
+ post_install_message: |
127
+ Thanks for installing yun!
128
+ You need put a config file in your home directory: ~/.yun
129
+ :default:
130
+ :aws_access_key_id: YOUR_AWS_ACCESS_KEY_ID
131
+ :aws_secret_access_key: YOUR_AWS_SECRET_ACCESS_KEY
132
+ :region: us-west-1
133
+ :key_name: YOUR_KEY_PAIR_NAME
134
+ :chef_repo: /PATH/TO/YOUR/CHEF_REPO
135
+
119
136
  rdoc_options: []
120
137
 
121
138
  require_paths: