Class: Shuttle::Wordpress

Inherits:
Php show all
Includes:
WordpressCli, WordpressCore, WordpressPlugins, WordpressVip
Defined in:
lib/shuttle/deployment/wordpress.rb

Constant Summary

Constants included from WordpressVip

Shuttle::WordpressVip::VIP_URL

Constants included from WordpressCli

Shuttle::WordpressCli::CLI_GIT, Shuttle::WordpressCli::CLI_PATH

Constants included from Helpers

Helpers::LEVEL_COLORS

Instance Attribute Summary

Attributes inherited from Deploy

#config, #environment, #ssh, #target, #version

Instance Method Summary collapse

Methods included from WordpressPlugins

#plugin_custom_install, #plugin_install, #plugin_installed?

Methods included from WordpressVip

#vip_get_config, #vip_install, #vip_installed?, #vip_link, #vip_path, #vip_required?, #vip_update

Methods included from WordpressCore

#core_install, #core_installed?, #core_path, #core_remove

Methods included from WordpressCli

#cli_checkout_code, #cli_install, #cli_installed?, #cli_uninstall

Methods inherited from Strategy

#changes_at?, #checkout_code, #cleanup_release, #cleanup_releases, #connect, #debug?, #deploy_running?, #disable_history, #execute_commands, #execute_hook, #export_environment, #keep_releases, #link_release, #release_lock, #rollback, #update_code, #update_code_svn, #write_lock, #write_release, #write_revision

Methods inherited from Deploy

#available_releases, #initialize, #last_version

Methods included from PathHelpers

#current_path, #deploy_path, #release_path, #scm_path, #shared_path, #version_path

Methods included from Helpers

#deployer_hostname, #error, #git_installed?, #git_remote, #log, #release_exists?, #stream_output, #svn_installed?, #warn

Constructor Details

This class inherits a constructor from Shuttle::Deploy

Instance Method Details

#activate_themeObject



209
210
211
212
213
214
215
216
# File 'lib/shuttle/deployment/wordpress.rb', line 209

def activate_theme
  name = config.wordpress.theme
  result = ssh.run("cd #{release_path} && wp theme activate #{name}")

  if result.failure?
    error "Unable to activate theme. Error: #{result.output}"
  end
end

#check_configObject



114
115
116
117
118
119
# File 'lib/shuttle/deployment/wordpress.rb', line 114

def check_config
  if !ssh.file_exists?(shared_path('wordpress/core/wp-config.php'))
    log "Creating wordpress config"
    generate_config
  end
end

#check_dependenciesObject



69
70
71
72
73
74
75
76
# File 'lib/shuttle/deployment/wordpress.rb', line 69

def check_dependencies
  if !svn_installed?
    log "Installing Subversion"
    if ssh.run("sudo apt-get install -y subversion").success?
      log "Subversion installed"
    end
  end
end

#check_pluginsObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/shuttle/deployment/wordpress.rb', line 178

def check_plugins
  plugins = config.wordpress.plugins

  if plugins
    if plugins.kind_of?(Array)
      plugins.each do |p|
        if p.kind_of?(String)
          plugin_install(p)
        elsif p.kind_of?(Hash)
          name, url = p.to_a.flatten.map(&:to_s)
          plugin_custom_install(name, url)
        end
      end
    else
      error "Config file has invalid plugins section"
    end
  end
end

#checkout_themeObject



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/shuttle/deployment/wordpress.rb', line 197

def checkout_theme
  if config.wordpress
    if config.wordpress.theme
      checkout_code("wp-content/themes/#{config.wordpress.theme}")
    else
      error "Theme name is not defined."
    end
  else
    error "Config does not contain 'wordpress' section"
  end
end

#deployObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/shuttle/deployment/wordpress.rb', line 46

def deploy
  setup
  update_code
  link_shared_data
  checkout_theme

  if vip_required?
    vip_install if !vip_installed?
    vip_link
  end

  if !site_installed?
    site_install
    network_install
  end

  check_plugins
  activate_theme

  link_release
  cleanup_releases
end

#generate_configObject



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

def generate_config
  mysql = config.wordpress.mysql
  if mysql.nil?
    error "Missing :mysql section of the config."
  end

  cmd = [
    "wp core config",
    "--dbname=#{mysql.database}",
    "--dbhost=#{mysql.host || 'localhost'}",
    "--dbuser=#{mysql.user}"
  ]

  cmd << "--dbpass=#{mysql.password}" if mysql.password

  res = ssh.run("cd #{core_path} && #{cmd.join(' ')}")
  if res.success?
    log "A new wordpress config has been generated"
  else
    error "Unable to generate config. Error: #{res.output}"
  end
end


170
171
172
173
174
175
176
# File 'lib/shuttle/deployment/wordpress.rb', line 170

def link_shared_data
  log "Linking shared data"

  ssh.run("cp -a #{core_path} #{release_path}")
  ssh.run("cp #{shared_path('wp-config.php')} #{release_path('wp-config.php')}")
  ssh.run("ln -s #{shared_path('wordpress/uploads')} #{release_path('wp-content/uploads')}")
end

#network_installObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/shuttle/deployment/wordpress.rb', line 154

def network_install
  if config.wordpress.network
    network = config.wordpress.network

    cmd = [
      "wp core install-network",
      "--title=#{network.title}",
    ].join(' ')

    result = ssh.run("cd #{release_path} && #{cmd}")
    if result.failure?
      error "Failed to setup WP network. #{result.output}"
    end
  end
end

#setupObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/shuttle/deployment/wordpress.rb', line 13

def setup
  if config.wordpress.nil?
    error "Please add :wordpress section to your config"
  end

  super

  setup_shared_dirs
  check_dependencies

  if !cli_installed?
    cli_install
  else
    version = ssh.capture("cd #{core_path} && wp --version")
    version.gsub!('wp-cli', '').to_s.strip!
    log "WordPress CLI version: #{version}"
  end

  if !core_installed?      
    core_install
  else
    res = ssh.run("cd #{core_path} && wp core version")

    if res.success?
      log "WordPress core version: #{res.output}"
    else
      warn "Unable to detect WordPress core version: #{res.output}"
    end
  end

  check_config
end

#setup_shared_dirsObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/shuttle/deployment/wordpress.rb', line 78

def setup_shared_dirs
  dirs = [
    'wordpress',
    'wordpress/uploads',
    'wordpress/core',
    'wordpress/plugins'
  ]

  dirs.each do |path|
    ssh.run("mkdir -p #{shared_path(path)}")
  end
end

#site_installObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/shuttle/deployment/wordpress.rb', line 132

def site_install
  if config.wordpress.site
    site = config.wordpress.site

    cmd = [
      "wp core install",
      "--url=#{site.url}",
      "--title=#{site.title.gsub(" ", "\\ ")}",
      "--admin_name=#{site.admin_name}",
      "--admin_email=#{site.admin_email}",
      "--admin_password=#{site.admin_password}"
    ].join(' ')

    result = ssh.run("cd #{release_path} && #{cmd}")
    if result.failure?
      error "Failed to setup site. #{result.output}"
    end
  else
    error "Please define :site section"
  end
end

#site_installed?Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
# File 'lib/shuttle/deployment/wordpress.rb', line 121

def site_installed?
  result = ssh.run("cd #{release_path} && wp core is-installed")

  if result.failure? && debug?
    log "Wordpress core check failed:"
    log result.output
  end

  result.success?
end