Class: Deployku::AppPlugin

Inherits:
Plugin
  • Object
show all
Includes:
Configurable, Containerable
Defined in:
lib/deployku/plugins/app.rb

Instance Attribute Summary

Attributes included from Configurable

#config

Instance Method Summary collapse

Methods included from Containerable

#container_id_path, #get_container_id, #get_status, #logs, #restart, #run, #set_container_id, #status, #stop

Methods included from Configurable

#config_get, #config_load, #config_path, #config_save, #config_set, #config_set_engine, #config_set_from, #config_set_port, #config_show, #config_unset, #config_unset_engine, #config_unset_from, #config_unset_port

Methods inherited from Plugin

<<, command_description, filter_plugins, find_plugin, help, inherited, instance, #packages, run

Constructor Details

#initializeAppPlugin

Returns a new instance of AppPlugin.



6
7
8
9
10
11
# File 'lib/deployku/plugins/app.rb', line 6

def initialize
  @config = {
    'env' => {},
    'links' => []
  }
end

Instance Method Details

#config_add_domain(app_name, domain) ⇒ Object



121
122
123
124
125
126
# File 'lib/deployku/plugins/app.rb', line 121

def config_add_domain(app_name, domain)
  config_load(app_name)
  @config['domains'] ||= []
  @config['domains'] << domain
  config_save(app_name)
end

#config_add_package(app_name, package) ⇒ Object



138
139
140
141
142
143
# File 'lib/deployku/plugins/app.rb', line 138

def config_add_package(app_name, package)
  config_load(app_name)
  @config['packages'] ||= []
  @config['packages'] << package
  config_save(app_name)
end

#config_add_volume(app_name, path) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/deployku/plugins/app.rb', line 155

def config_add_volume(app_name, path)
  config_load(app_name)
  vpath = volume_path(app_name, path)
  unless Dir.exists?(vpath)
    FileUtils.mkpath(vpath)
  end
  @config['volumes'] ||= {}
  @config['volumes'][vpath] = path unless @config['volumes'][vpath]
  config_save(app_name)
end

#config_rm_domain(app_name, package) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/deployku/plugins/app.rb', line 129

def config_rm_domain(app_name, package)
  config_load(app_name)
  if @config['domains']
    @config['domains'].delete(package)
    config_save(app_name)
  end
end

#config_rm_package(app_name, package) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/deployku/plugins/app.rb', line 146

def config_rm_package(app_name, package)
  config_load(app_name)
  if @config['packages']
    @config['packages'].delete(package)
    config_save(app_name)
  end
end

#config_rm_volume(app_name, path) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/deployku/plugins/app.rb', line 167

def config_rm_volume(app_name, path)
  config_load(app_name)
  vpath = volume_path(app_name, path)
  if @config['volumes']
    @config['volumes'].delete(vpath)
    config_save(app_name)
  end
end

#create(app_name) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/deployku/plugins/app.rb', line 14

def create(app_name)
  app_dir = dir(app_name)
  unless Dir.exists?(app_dir)
    Dir.mkdir(app_dir)
    system "git init -q --bare '#{app_dir}'"
  end
  app_dir
end

#delete(app_name) ⇒ Object



24
25
26
27
28
29
# File 'lib/deployku/plugins/app.rb', line 24

def delete(app_name)
  app_dir = dir(app_name)
  if Dir.exists?(app_dir)
    FileUtils.rm_rf(app_dir)
  end
end

#dir(app_name) ⇒ Object



198
199
200
# File 'lib/deployku/plugins/app.rb', line 198

def dir(app_name)
  File.join(Deployku::Config.home, Deployku.sanitize_app_name(app_name))
end

#get_app_listObject



202
203
204
205
206
207
208
# File 'lib/deployku/plugins/app.rb', line 202

def get_app_list
  apps = []
  Dir.glob(File.join(Deployku::Config.home, '*')) do |path|
    apps << File.basename(path) if File.directory?(path)
  end
  apps
end


177
178
179
180
181
182
# File 'lib/deployku/plugins/app.rb', line 177

def link(app_name, link)
  config_load(app_name)
  @config['links'] ||= []
  @config['links'] << link
  config_save(app_name)
end

#listObject



32
33
34
35
# File 'lib/deployku/plugins/app.rb', line 32

def list
  apps = get_app_list
  apps.each { |app| puts app }
end

#rebuild(app_name, new_rev = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/deployku/plugins/app.rb', line 54

def rebuild(app_name, new_rev=nil)
  app_dir = dir(app_name)
  app_name = File.basename(app_dir)
  Dir.mktmpdir('deployku_') do |tmp_dir|
    tmp_app_dir = File.join(tmp_dir, 'app')
    Dir.mkdir(tmp_app_dir)
    system "git clone -q '#{app_dir}' '#{tmp_app_dir}'"
    Dir.chdir(tmp_app_dir)
    system "unset GIT_DIR GIT_WORK_TREE && git checkout #{new_rev}" if new_rev
    system 'unset GIT_DIR GIT_WORK_TREE && git submodule update --init --recursive'
    Dir.chdir(tmp_dir)

    plugins = Deployku::Plugin.filter_plugins(:detect)
    plugin = nil
    plugins.each do |plug|
      if plug.detect(tmp_app_dir)
        plugin = plug
        break
      end
    end

    deployku_config_path = File.join(tmp_app_dir, 'deployku.yml')
    Deployku::Config.load(deployku_config_path)
    Deployku::Config.load(config_path(app_name))
    config_load(app_name)

    unless plugin
      puts "Unsupported application type"
      exit 1
    else
      if plugin.respond_to?(:port) && !@config['port']
        config_set_port(app_name, plugin.port)
        Deployku::Config.merge!({ port: plugin.port })
      end

      plugin.build_start_script(tmp_dir)
      case Deployku::Config.engine
        when 'docker'
          plugin.build_dockerfile(tmp_dir)
        when 'lxc'
          puts 'not implemented'
      end
    end

    if Deployku::Engine.rebuild(app_name, tmp_dir)
      plugin.volumes.each do |volume|
        config_add_volume(app_name, volume)
      end if plugin.respond_to?(:volumes)

      restart(app_name)
    else
      exit 1
    end
  end
end

#start(app_name) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/deployku/plugins/app.rb', line 39

def start(app_name)
  super(app_name)
  result = true
  plugins = Deployku::Plugin.filter_plugins(:after_start)
  plugins.each do |plug|
    result = result && plug.after_start(app_name)
  end
  result
end


185
186
187
188
189
190
191
# File 'lib/deployku/plugins/app.rb', line 185

def unlink(app_name, link)
  config_load(app_name)
  if @config['links']
    @config['links'].delete(link)
    config_save(app_name)
  end
end

#volume_path(app_name, path) ⇒ Object



193
194
195
196
# File 'lib/deployku/plugins/app.rb', line 193

def volume_path(app_name, path)
  volume = Deployku.sanitize_app_name(path)
  File.join(dir(app_name), 'DEPLOYKU_VOLUMES', volume)
end