Class: DEIS::Deploy

Inherits:
Thor
  • Object
show all
Extended by:
Utils
Includes:
Utils
Defined in:
lib/deis_deploy/deploy.rb

Instance Method Summary collapse

Methods included from Utils

config_data, data_to_deis_config_set, file_store, target_host

Instance Method Details

#checkout(env, host = nil, config = nil) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/deis_deploy/deploy.rb', line 145

def checkout(env, host=nil, config=nil)
  host = self.target_host(env) if host.nil?
  config = DEIS::Config.current(env) if config.nil? || config.class != Hash
  # presume you have a working ssh config so we dont need to worry about the ssh user
  ssh = SSH.new({:host => host, :user => nil})
  # check base location exists
  base = ssh.exec!("ls -lart #{DEIS::REMOTE_APP_DIR} | wc -l").to_i
  # create if it doesnt
  ssh.exec!("mkdir -p #{DEIS::REMOTE_APP_DIR}") if base == 0
  # check location
  location = ssh.exec!("ls -lart #{DEIS::REMOTE_APP_DIR}#{config['dir']} | wc -l").to_i
  # clone if it doesn't exist
  ssh.exec!("cd #{DEIS::REMOTE_APP_DIR} && git clone #{config['remote']} #{config['deis_name']}") if location == 0
  # find current ref head
  ref = DEIS::Git.branch
  "current git ref: #{ref}".log
  # checkout to that ref head
  res = nil
  res = ssh.exec!("cd #{DEIS::REMOTE_APP_DIR}#{config['dir']} && git fetch && git checkout #{ref}") unless ref.nil?
  ssh.close
  res
end

#deis_config(env, host = nil, config = nil) ⇒ Object



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

def deis_config(env, host=nil, config=nil)
  host = self.target_host(env) if host.nil?
  config = DEIS::Config.current(env) if config.nil? || config.class != Hash
  "Setting config data..".info
  # presume you have a working ssh config so we dont need to worry about the ssh user
  ssh = SSH.new({:host => host, :user => nil})
  location = "#{DEIS::REMOTE_APP_DIR}#{config['dir']}"
  # generate deis config string from the config data
  config_cmd = self.data_to_deis_config_set(config['config'])
  res = ssh.exec!("cd #{location} && #{config_cmd}") unless config_cmd.nil?
  ssh.close
  if res.nil? && ! config_cmd.nil?
    "Config command failed".fatal
    exit 1
  elsif config_cmd.nil?
    "No config data to set".info
  end
end

#deis_create(env, host = nil, config = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/deis_deploy/deploy.rb', line 111

def deis_create(env, host=nil, config=nil)
  host = self.target_host(env) if host.nil?
  config = DEIS::Config.current(env) if config.nil? || config.class != Hash

  "Creating deis app".info
  # presume you have a working ssh config so we dont need to worry about the ssh user
  ssh = SSH.new({:host => host, :user => nil})
  location = "#{DEIS::REMOTE_APP_DIR}#{config['dir']}"
  # check if already created the app
  app_exists = ssh.exec!("deis apps | grep #{config['deis_name']} | wc -l").to_i
  creation_failed = 0
  # if the app does exist, create it
  if app_exists == 0
    creation_failed = ssh.exec!("cd #{location} && deis create #{config['deis_name']} --cluster=#{config['env']} | grep 400 | wc -l").to_i
  else
    "Application alrady exists..".info
  end
  # if creation failed, die
  if creation_failed >= 1
    "Failed to create deis app".fatal
    "\nThis could be due to cluster being missing, the application name may be in use or other issues.".warning
    exit 1
  end
  # make sure this dir uses this app name
  matches = ssh.exec!("sleep 1; cd #{location} && deis info | grep '#{config['deis_name']} Application' -o  | wc -l").to_i
  if matches != 1
    "Application names do not match".fatal
    exit 1
  end
  ssh.close

end

#deis_domain(env, host = nil, config = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/deis_deploy/deploy.rb', line 53

def deis_domain(env, host=nil, config=nil)
  host = self.target_host(env) if host.nil?
  config = DEIS::Config.current(env) if config.nil? || config.class != Hash
  "Adding domain..".info
  ssh = SSH.new({:host => host, :user => nil})
  location = "#{DEIS::REMOTE_APP_DIR}#{config['dir']}"
  if config.has_key?("url") && config["url"].has_key?(env)
    domains = config['url'][env]
    domains.each do |domain|
      exists = ssh.exec!("cd #{location} && deis domains | grep #{domain} -o | wc -l").to_i
      res = ssh.exec!("cd #{location} && deis domains:add #{domain} | grep 400 | wc -l").to_i if exists == 0
      "Domain #{domain} already in use".warning if ! res.nil? && res > 0
      "Added #{domain}".info if ! res.nil? && res == 0
    end
  else
    "No custom url to add".info
  end
  ssh.close

end

#deis_unset_config(env, host = nil, config = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/deis_deploy/deploy.rb', line 75

def deis_unset_config(env, host=nil, config=nil)
  host = self.target_host(env) if host.nil?
  config = DEIS::Config.current(env) if config.nil? || config.class != Hash
  name = DEIS::Config.full_name(env)
  "Checking for old config data to remove..".info
  ssh = SSH.new({:host => host, :user => nil})
  location = "#{DEIS::REMOTE_APP_DIR}#{config['dir']}"
  # run the unset calls
  config['removed_config'].each{|r| ssh.exec!( "cd #{location} && deis config:unset #{r['k']}" ) }
  ssh.close
  config['removed_config'] = []
  DEIS::Config.save_current(env, config, name)
  "Removed".info
end

#push(env, host = nil, config = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/deis_deploy/deploy.rb', line 36

def push(env, host=nil, config=nil)
  host = self.target_host(env) if host.nil?
  config = DEIS::Config.current(env) if config.nil? || config.class != Hash
  "Deploying..".info
  ssh = SSH.new({:host => host, :user => nil})
  location = "#{DEIS::REMOTE_APP_DIR}#{config['dir']}"
  ref = DEIS::Git.ref
  branch = DEIS::Git.branch
  # actually deploys it
  res = ssh.exec!("cd #{location} && git push deis #{branch}:#{ref}")
  "Failed to deploy".fatal if res == false
  up = ssh.exec!("cd #{location} && deis scale web=1 | grep 'web\..* up' | wc -l").to_i if res != false
  "Complete".important if ! up.nil? && up > 0
  ssh.close
end

#stagingObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/deis_deploy/deploy.rb', line 8

def staging
  env = "staging"
  name = DEIS::Config.name
  host = self.target_host(env)
  "Deploying '#{name}' to '#{env}' on '#{host}'".info
  # fetch config data for this name
  config = DEIS::Config.current(env)
  # run validations
  DEIS::Validations.deploy(env, host, config)
  if ! config.nil?
    # clone repo to $HOME/APPS/$FULL_NAME (swap if exists - add to saved config data)
    # fetch
    # checkout to matching branch
    self.checkout(env,host, config)
    # deis create $name
    self.deis_create(env, host, config)
    # add domain
    self.deis_domain(env, host, config)
    # create deis config command and run it
    self.deis_unset_config(env, host, config)
    self.deis_config(env, host, config)

    # git push deis $branch
    self.push(env, host, config)
  end
end