sunzi 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,17 +26,17 @@ Go to your project directory, then:
26
26
 
27
27
  $ sunzi create
28
28
 
29
- It generates a `sunzi` folder, subdirectories and some templates for you. Inside `sunzi`, there's `here` folder, which will be kept in your local machine, that contains some scripts and definition files. Also there's `there` folder, which will be transferred to the remote server, that contains recipes and dynamic variables compiled from the definition files in the `here` folder.
29
+ It generates a `sunzi` folder along with subdirectories and templates. Inside `sunzi`, there's `attributes.yml`, which defines dynamic attributes to be used from recipes. Also there's the `remote` folder, which will be transferred to the remote server, that contains recipes and dynamic variables compiled from `attributes.yml`.
30
30
 
31
- Go into the `here` directory, then run the `deploy.sh`:
31
+ Go into the `sunzi` directory, then run the `sunzi deploy`:
32
32
 
33
- $ cd sunzi/here
34
- $ bash deploy.sh root@example.com
33
+ $ cd sunzi
34
+ $ sunzi deploy root@example.com
35
35
 
36
36
  Now, what it actually does is:
37
37
 
38
38
  1. SSH to `example.com` and login as `root`
39
- 1. Transfer the content of the `there` directory to the remote server and extract in `$HOME/sunzi`
39
+ 1. Transfer the content of the `remote` directory to the remote server and extract in `$HOME/sunzi`
40
40
  1. Run `install.sh` in the remote server
41
41
 
42
42
  As you can see, what you need to do is edit `install.sh` and add some shell commands. That's it.
@@ -48,23 +48,22 @@ Here's the directory structure that `sunzi create` automatically generates:
48
48
 
49
49
  ```
50
50
  sunzi/
51
- here/ ---- kept in your local machine
52
- attributes.yml ---- add custom variables here
53
- compile.rb ---- compile the content of attributes.yml to there/attributes/*
54
- deploy.sh ---- invoke this script
55
- there/ ---- transferred to the remote server
56
- attributes/ ---- compiled from attributes.yml at deploy
51
+ attributes.yml ---- add custom variables here
52
+ remote/ ---- everything under this folder will be transferred to the remote server
53
+ attributes/ ---- compiled from attributes.yml at deploy
57
54
  env
58
55
  ssh_key
59
- recipes/ ---- put commonly used scripts here, referred from install.sh
56
+ recipes/ ---- put commonly used scripts here, referred from install.sh
60
57
  ssh_key.sh
61
- install.sh ---- main scripts that gets run on the remote server
58
+ install.sh ---- main scripts that gets run on the remote server
62
59
  ```
63
60
 
64
61
  Vagrant
65
62
  -------
66
63
 
67
- If you're using Sunzi with [Vagrant](http://vagrantup.com/), make sure you allowed SSH access for root, then:
64
+ If you're using Sunzi with [Vagrant](http://vagrantup.com/), make sure you allowed SSH access for root.
65
+
66
+ Since it uses port 2222 for SSH, you need to specify the port number:
68
67
 
69
68
  $ vagrant up
70
- $ bash deploy.sh root@localhost -p 2222
69
+ $ sunzi deploy root@localhost 2222
@@ -1,4 +1,7 @@
1
1
  require 'thor'
2
+ require 'yaml'
3
+ require 'fileutils'
4
+ require 'open3'
2
5
 
3
6
  module Sunzi
4
7
  CONFIG_DIR = File.join(ENV['HOME'],'.config','sunzi')
@@ -13,18 +16,64 @@ module Sunzi
13
16
  end
14
17
 
15
18
  map "c" => :create
19
+ map "d" => :deploy
16
20
 
17
21
  desc "create [PROJECT]", "Create sunzi project (Shortcut: c)"
18
22
  def create(project = 'sunzi')
19
23
  empty_directory project
20
- empty_directory "#{project}/here"
21
- empty_directory "#{project}/there"
22
- empty_directory "#{project}/there/recipes"
23
- template "templates/here/attributes.yml", "#{project}/here/attributes.yml"
24
- template "templates/here/compile.rb", "#{project}/here/compile.rb"
25
- template "templates/here/deploy.sh", "#{project}/here/deploy.sh"
26
- template "templates/there/install.sh", "#{project}/there/install.sh"
27
- template "templates/there/recipes/ssh_key.sh", "#{project}/there/recipes/ssh_key.sh"
24
+ empty_directory "#{project}/remote"
25
+ empty_directory "#{project}/remote/recipes"
26
+ template "templates/attributes.yml", "#{project}/attributes.yml"
27
+ template "templates/remote/install.sh", "#{project}/remote/install.sh"
28
+ template "templates/remote/recipes/ssh_key.sh", "#{project}/remote/recipes/ssh_key.sh"
28
29
  end
30
+
31
+ desc "deploy [USER@HOST] [PORT]", "Deploy sunzi project (Shortcut: d)"
32
+ def deploy(*target)
33
+ if target.empty? or !target.first.match(/@/)
34
+ puts "Usage: sunzi deploy root@example.com"
35
+ abort
36
+ end
37
+
38
+ # Compile attributes.yml
39
+ unless File.exists?('attributes.yml')
40
+ puts "You must be in the sunzi folder"
41
+ abort
42
+ end
43
+ hash = YAML.load(File.read('attributes.yml'))
44
+ FileUtils.mkdir_p('remote/attributes')
45
+ hash.each do |key, value|
46
+ File.open("remote/attributes/#{key}", 'w'){|file| file.write(value) }
47
+ end
48
+
49
+ host, port = target
50
+ port ||= 22
51
+ user, domain = host.split('@')
52
+
53
+ commands = <<-EOS
54
+ ssh-keygen -R #{domain}
55
+ cd remote
56
+ tar cz . | ssh -o 'StrictHostKeyChecking no' #{host} -p #{port} '
57
+ rm -rf ~/sunzi &&
58
+ mkdir ~/sunzi &&
59
+ cd ~/sunzi &&
60
+ tar xz &&
61
+ bash install.sh'
62
+ EOS
63
+
64
+ Open3.popen3(commands) do |stdin, stdout, stderr|
65
+ stdin.close
66
+ t = Thread.new(stderr) do |terr|
67
+ while (line = terr.gets)
68
+ print "\e[31m#{line}\e[0m"
69
+ end
70
+ end
71
+ while (line = stdout.gets)
72
+ print "\e[32m#{line}\e[0m"
73
+ end
74
+ t.join
75
+ end
76
+ end
77
+
29
78
  end
30
79
  end
@@ -1,3 +1,3 @@
1
1
  module Sunzi
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,3 +1,3 @@
1
- --- # Dynamic variables here will be compiled to individual files in there/attributes.
1
+ --- # Dynamic variables here will be compiled to individual files in remote/attributes.
2
2
  env: production
3
3
  ssh_key: id_rsa.pub
@@ -11,7 +11,9 @@ else
11
11
  chmod 700 ~/.ssh
12
12
  chmod 600 ~/.ssh/authorized_keys
13
13
  else
14
- echo 'The public key file ($1) not found! Copy it from $HOME/.ssh to the "there" directory.'
15
- echo 'If the file name found in .ssh is different from $1, edit here/attributes.yml as appropriate.'
14
+ echo "The public key file is not found! Try the following command:"
15
+ echo "cp ~/.ssh/$1 remote"
16
+ echo "If the file name found in ~/.ssh is different from \"$1\", edit attributes.yml as appropriate."
17
+ exit 1
16
18
  fi
17
19
  fi
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.email = ["kenn.ejima@gmail.com"]
10
10
  s.homepage = "http://github.com/kenn/sunzi"
11
11
  s.summary = %q{Server provisioning tool for minimalists}
12
- s.description = %q{The supreme art of war is to subdue the enemy without fighting.}
12
+ s.description = %q{Server provisioning tool for minimalists}
13
13
 
14
14
  s.rubyforge_project = "sunzi"
15
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sunzi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
16
- requirement: &2160818440 !ruby/object:Gem::Requirement
16
+ requirement: &2160735500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,8 +21,8 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2160818440
25
- description: The supreme art of war is to subdue the enemy without fighting.
24
+ version_requirements: *2160735500
25
+ description: Server provisioning tool for minimalists
26
26
  email:
27
27
  - kenn.ejima@gmail.com
28
28
  executables:
@@ -39,11 +39,9 @@ files:
39
39
  - lib/sunzi/base.rb
40
40
  - lib/sunzi/cli.rb
41
41
  - lib/sunzi/version.rb
42
- - lib/templates/here/attributes.yml
43
- - lib/templates/here/compile.rb
44
- - lib/templates/here/deploy.sh
45
- - lib/templates/there/install.sh
46
- - lib/templates/there/recipes/ssh_key.sh
42
+ - lib/templates/attributes.yml
43
+ - lib/templates/remote/install.sh
44
+ - lib/templates/remote/recipes/ssh_key.sh
47
45
  - sunzi.gemspec
48
46
  homepage: http://github.com/kenn/sunzi
49
47
  licenses: []
@@ -1,11 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'yaml'
4
- hash = YAML.load(File.read('attributes.yml'))
5
-
6
- require 'fileutils'
7
- FileUtils.mkdir_p('../there/attributes')
8
-
9
- hash.each do |key, value|
10
- File.open("../there/attributes/#{key}", 'w'){|file| file.write(value) }
11
- end
@@ -1,25 +0,0 @@
1
- #!/bin/bash
2
-
3
- # Usage: bash deploy.sh [host] [-p 2222]
4
-
5
- if [ -z "$1" ]; then
6
- echo "Usage: bash deploy.sh user@example.com"
7
- exit 1
8
- fi
9
-
10
- # Compile attributes
11
- ruby compile.rb
12
-
13
- # The host key might change when we instantiate a new VM, so
14
- # we remove (-R) the old host key from known_hosts
15
- host="$1"
16
- ssh-keygen -R "${host#*@}" 2> /dev/null
17
-
18
- # Connect to the remote server and deploy
19
- cd ../there
20
- tar cz . | ssh -o 'StrictHostKeyChecking no' "$host" "$2" "$3" '
21
- rm -rf ~/sunzi &&
22
- mkdir ~/sunzi &&
23
- cd ~/sunzi &&
24
- tar xz &&
25
- bash install.sh'