Module: Unfold

Defined in:
lib/unfold.rb

Defined Under Namespace

Classes: Config

Class Method Summary collapse

Class Method Details

.config(env_arrays) ⇒ Object

the method used in the config file



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/unfold.rb', line 8

def self.config(env_arrays)
  return nil if block_given? == false
  configs = []
  env_arrays.each do |env|
    config = Config.new
    config.env = env.to_s
    yield(config,env.to_s)
    configs << config
  end
  return configs
end

.post_receive_script(c) ⇒ Object

creates a post-receive hook script from an Unfold::Config object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/unfold.rb', line 25

def self.post_receive_script(c)
  log_location = "~/.unfold/logs/#{c.appname}-#{c.env}/deploy.log"
  postdep = "#{c.remote_destination}/config/unfold_postdeploy.rb"
  return %{
    #!/bin/bash
    read oldrev newrev refname
    
    deploy() {
      if [ "$refname" = "refs/heads/master" ]; then
        unset GIT_DIR
        echo "Unfold: Deploying $newrev" >> #{log_location}
        cd #{c.remote_destination}
        pwd >> #{log_location}
        git pull origin master > /dev/null 2> /dev/null
        echo "Unfold: SUCCESS! Deployed $newrev" >> #{log_location}
        if [ -f #{postdep} ]; then
          bash -lc "ruby #{postdep}"
        fi
      fi
    }
    deploy
  }
end

.read_configObject



49
50
51
# File 'lib/unfold.rb', line 49

def self.read_config
  eval(File.read("#{`git rev-parse --show-toplevel`.strip}/config/unfold.rb"))
end

.rollback_to(envi, revision) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/unfold.rb', line 73

def self.rollback_to(envi, revision)
  Unfold.read_config.each do |c|
    if c.env == envi
      Unfold.ssh c do |ssh, paths| 
        print ssh.exec!("cd #{c.remote_destination}; pwd; git reset --hard #{revision}")
      end
    end
  end
end

.setup_localObject



83
84
85
86
87
88
89
# File 'lib/unfold.rb', line 83

def self.setup_local
  # set up the local git repo's remotes
  Unfold.read_config.each do |c|
    repo_path = "~/.unfold/repos/#{c.appname}-#{c.env}.git"
    `git remote add #{c.env} #{c.remote_user}@#{c.remote_host}:#{repo_path}`
  end
end

.setup_remoteObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/unfold.rb', line 91

def self.setup_remote()
  Unfold.read_config.each do |c|
    Unfold.ssh c do |ssh, paths|
      # create the remote repo
      ssh.exec!("mkdir -p #{paths[:repo]}; mkdir -p #{paths[:logs]}")
      ssh.exec!("cd #{paths[:repo]}; git --bare init")
      
      # upload the post-receive hook
      script = StringIO.new(Unfold.post_receive_script(c))
      dest = "#{paths[:repo]}/hooks/post-receive"
      ssh.scp.upload!(script, dest)
      
      # make it executable
      ssh.exec!("chmod 775 #{paths[:repo]}/hooks/post-receive")
      
      # move to the deployment target and locally clone the repo
      ssh.exec!("cd #{c.remote_destination}; git clone #{paths[:repo]} .")
    end
  end
end

.ssh(c) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/unfold.rb', line 53

def self.ssh(c)
  return nil if block_given? == false
  Net::SSH.start(c.remote_host, c.remote_user) do |ssh|
    # get the home directory
    path = ssh.exec!("cd; pwd").strip
    
    # setup paths
    repo_path = "#{path}/.unfold/repos/#{c.appname}-#{c.env}.git" # doesn't include ~/ so that it can be used in the remote url
    logs_path = "#{path}/.unfold/logs/#{c.appname}-#{c.env}/"
    yield(ssh,{ :repo => repo_path, :logs => logs_path })
  end
end

.teardown(local, remote) ⇒ Object



66
67
68
69
70
71
# File 'lib/unfold.rb', line 66

def self.teardown(local, remote)
  Unfold.read_config.each do |c|
    `git remote remote #{c.env}` if local == true
    Unfold.ssh c { |ssh, paths| ssh.exec!("rm -rf #{paths[:repo]}; rm -rf #{paths[:logs]}") } if remote == true
  end
end