unfold 1.0.1 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/uf +35 -2
  3. data/lib/unfold.rb +55 -43
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cac5b5e9580da7fa1d53e368a9973630601016eb
4
- data.tar.gz: e880c9effeff80330533fb1d5d0d2a6d4d8f43af
3
+ metadata.gz: acfd463411021cc886e7183e6e7eddb009715b42
4
+ data.tar.gz: 3e04e322affa8f3404a01fe576154427f452a0e6
5
5
  SHA512:
6
- metadata.gz: e873293ca34d69abb1490c8297ac7f5a9a43c29362c47dea0a24d574c757989903313e7fc7b5400b5ae670bc402719e2688da66c67a97f83b9e895e05ec188d5
7
- data.tar.gz: 8343056f19ac5ac04f5e7b355dd44edbfebe384c4f6315e5d625109dd27a740bdb67fd0efa2cddcdf922be236807e3b80842fe0fe76740ea5f2b12c86da6e69e
6
+ metadata.gz: 2c54f52a934eb52e4c24a561c76309c6eaf9606b408991cb6730c7480cd6114c9361a45016450d450c7d093f7856820ce8ac47705bab549a2598eae4f0bbab70
7
+ data.tar.gz: 1dd4ffde2821b8df669fd3db9b9f5f96d4e882b199f4bc832c7df1e2334ab6ff972c66c923753b770d66f4ed179c4f304b7cc1c3d9c49d91f4da9574d8af30da
data/bin/uf CHANGED
@@ -8,9 +8,42 @@ when "setup"
8
8
  Unfold.setup_remote()
9
9
  when "local"
10
10
  Unfold.setup_local()
11
+ when "both"
12
+ Unfold.setup_remote()
13
+ Unfold.setup_local()
14
+ else
15
+ puts "Unfold: Invalid setup option."
16
+ end
17
+ when "teardown"
18
+ case ARGV[1]
19
+ when "local"
20
+ Unfold.teardown(true, false)
21
+ when "server"
22
+ Unfold.teardown(false, true)
23
+ when "both"
24
+ Unfold.teardown(true, true)
25
+ else
26
+ puts "Unfold: Invalid teardown option."
27
+ end
28
+ when "rollback"
29
+ env = ARGV[1].to_s
30
+ revision = ARGV[2].to_s
31
+ if revision.length > 0
32
+ Unfold.rollback_to(env, revision)
11
33
  else
12
- puts "Invalid setup location."
34
+ puts "Unfold: Invalid revision."
13
35
  end
14
36
  else
15
- puts "Invalid command."
37
+ print %{uf <command> <options>\n
38
+ commands & options:
39
+ setup Sets up unfold
40
+ local Sets up the local git repo.
41
+ remote Sets up the servers specified in config/unfold.rb
42
+ both Does both local and remote setup
43
+ teardown Removes all traces of Unfold from any servers in config/unfold.rb
44
+ local Removes local setup
45
+ server Removes logs and repo in ~/.unfold on any servers in config/unfold.rb
46
+ both Remotes local and remote setup
47
+ rollback Rolls back the deployment repo to a revision using
48
+ <revision> The revision SHA1 hash to rollback to\n}
16
49
  end
data/lib/unfold.rb CHANGED
@@ -4,7 +4,6 @@ require "net/scp"
4
4
  require "stringio"
5
5
 
6
6
  module Unfold
7
-
8
7
  # the method used in the config file
9
8
  def self.config(env_arrays)
10
9
  return nil if block_given? == false
@@ -20,79 +19,92 @@ module Unfold
20
19
 
21
20
  class Config
22
21
  attr_accessor :remote_host, :remote_user, :remote_destination, :appname, :env
23
-
24
- # hashify config object
25
- # not used anywhere
26
- def to_hash
27
- hash = {}
28
- self.instance_variables.each do |ivar|
29
- hash[ivar.to_s.delete("@")] = self.instance_variable_get(ivar)
30
- end
31
- return hash
32
- end
33
22
  end
34
23
 
35
- # creates a post-receive hook script from a configuration
24
+ # creates a post-receive hook script from an Unfold::Config object
36
25
  def self.post_receive_script(c)
37
26
  log_location = "~/.unfold/logs/#{c.appname}-#{c.env}/deploy.log"
38
27
  postdep = "#{c.remote_destination}/config/unfold_postdeploy.rb"
39
28
  return %{
40
- #!/bin/bash\n
41
- read oldrev newrev refname\n
42
- if [ "$refname" = "refs/heads/master" ]; then\n
43
- unset GIT_DIR\n
44
- echo "Unfold: Deploying $newrev" >> #{log_location}\n
45
- cd #{c.remote_destination}; git pull origin master\n
46
- echo "Unfold: SUCCESS! Deployed $newrev" >> #{log_location}\n
47
- if [ -e #{postdep} ]; then\n
48
- bash -lc "ruby #{postdep}"\n
49
- fi\n
29
+ #!/bin/bash
30
+ read oldrev newrev refname
31
+
32
+ deploy() {
33
+ if [ "$refname" = "refs/heads/master" ]; then
34
+ unset GIT_DIR
35
+ echo "Unfold: Deploying $newrev" >> #{log_location}
36
+ cd #{c.remote_destination}
37
+ pwd >> #{log_location}
38
+ git pull origin master > /dev/null 2> /dev/null
39
+ echo "Unfold: SUCCESS! Deployed $newrev" >> #{log_location}
40
+ if [ -f #{postdep} ]; then
41
+ bash -lc "ruby #{postdep}"
42
+ fi
50
43
  fi
44
+ }
45
+ deploy
51
46
  }
52
47
  end
53
48
 
54
49
  def self.read_config
55
50
  eval(File.read("#{`git rev-parse --show-toplevel`.strip}/config/unfold.rb"))
56
51
  end
52
+
53
+ def self.ssh(c)
54
+ return nil if block_given? == false
55
+ Net::SSH.start(c.remote_host, c.remote_user) do |ssh|
56
+ # get the home directory
57
+ path = ssh.exec!("cd; pwd").strip
58
+
59
+ # setup paths
60
+ repo_path = "#{path}/.unfold/repos/#{c.appname}-#{c.env}.git" # doesn't include ~/ so that it can be used in the remote url
61
+ logs_path = "#{path}/.unfold/logs/#{c.appname}-#{c.env}/"
62
+ yield(ssh,{ :repo => repo_path, :logs => logs_path })
63
+ end
64
+ end
65
+
66
+ def self.teardown(local, remote)
67
+ Unfold.read_config.each do |c|
68
+ `git remote remote #{c.env}` if local == true
69
+ Unfold.ssh c { |ssh, paths| ssh.exec!("rm -rf #{paths[:repo]}; rm -rf #{paths[:logs]}") } if remote == true
70
+ end
71
+ end
72
+
73
+ def self.rollback_to(envi, revision)
74
+ Unfold.read_config.each do |c|
75
+ if c.env == envi
76
+ Unfold.ssh c do |ssh, paths|
77
+ print ssh.exec!("cd #{c.remote_destination}; pwd; git reset --hard #{revision}")
78
+ end
79
+ end
80
+ end
81
+ end
57
82
 
58
83
  def self.setup_local
59
- configs = Unfold.read_config
60
-
61
84
  # set up the local git repo's remotes
62
- configs.each do |c|
85
+ Unfold.read_config.each do |c|
63
86
  repo_path = "~/.unfold/repos/#{c.appname}-#{c.env}.git"
64
87
  `git remote add #{c.env} #{c.remote_user}@#{c.remote_host}:#{repo_path}`
65
88
  end
66
89
  end
67
90
 
68
91
  def self.setup_remote()
69
- # load configs from config file
70
- configs = Unfold.read_config
71
-
72
- configs.each do |c|
73
- # SSH into the remote
74
- Net::SSH.start(c.remote_host, c.remote_user) do |ssh|
75
- # get the home directory
76
- path = ssh.exec!("cd; pwd").strip
77
-
78
- # setup
79
- repo_path = "#{path}/.unfold/repos/#{c.appname}-#{c.env}.git" # doesn't include ~/ so that it can be used in the remote url
80
- logs_path = "#{path}/.unfold/logs/#{c.appname}-#{c.env}/"
81
-
92
+ Unfold.read_config.each do |c|
93
+ Unfold.ssh c do |ssh, paths|
82
94
  # create the remote repo
83
- ssh.exec!("mkdir -p #{repo_path}; mkdir -p #{logs_path}")
84
- ssh.exec!("cd #{repo_path}; git --bare init")
95
+ ssh.exec!("mkdir -p #{paths[:repo]}; mkdir -p #{paths[:logs]}")
96
+ ssh.exec!("cd #{paths[:repo]}; git --bare init")
85
97
 
86
98
  # upload the post-receive hook
87
99
  script = StringIO.new(Unfold.post_receive_script(c))
88
- dest = "#{repo_path.sub("~",path)}/hooks/post-receive"
100
+ dest = "#{paths[:repo]}/hooks/post-receive"
89
101
  ssh.scp.upload!(script, dest)
90
102
 
91
103
  # make it executable
92
- ssh.exec!("chmod 775 #{repo_path}/hooks/post-receive")
104
+ ssh.exec!("chmod 775 #{paths[:repo]}/hooks/post-receive")
93
105
 
94
106
  # move to the deployment target and locally clone the repo
95
- ssh.exec!("cd #{c.remote_destination}; git clone #{repo_path} .")
107
+ ssh.exec!("cd #{c.remote_destination}; git clone #{paths[:repo]} .")
96
108
  end
97
109
  end
98
110
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unfold
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathaniel Symer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-03 00:00:00.000000000 Z
11
+ date: 2014-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh