Class: Strobe::Resources::Application

Inherits:
Object
  • Object
show all
Includes:
Strobe::Resource::Collection, Strobe::Retryable
Defined in:
lib/strobe/resources/application.rb

Defined Under Namespace

Classes: PackFile

Instance Attribute Summary

Attributes included from Strobe::Resource::Base

#response

Instance Method Summary collapse

Methods included from Strobe::Retryable

#retry_on_error

Methods included from Strobe::Resource::Collection

#key, #reload, #reload!

Methods included from Strobe::Resource::Base

#[], #[]=, #destroy, #initialize, #key?, #merge!, #params, #params=, #persisted?, #save, #save!

Methods included from Validations

#read_attribute_for_validation, #valid_attribute?, #valid_for_given_attributes?

Instance Method Details

#deploy!(opts = {}) ⇒ Object


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/strobe/resources/application.rb', line 89

def deploy!(opts = {})
  self['path'] = opts[:path] if opts[:path]

  if opts[:pre_deploy]
    unless Kernel.system(opts[:pre_deploy])
      raise Strobe::DeployInterrupted, "pre deploy hook returned non-zero status, deploy was stopped"
    end
  end

  # TODO: trappist and/or monastery freaks out when we pass production environment
  #       explicitly, we should fix this as it's not intuitive
  environment  = (opts[:environment] == "production" ? nil : opts[:environment])
  callback     = opts[:callback]
  message      = opts[:message]
  github_url   = opts[:github_url]
  scm_version  = opts[:scm_version]

  validate_for_deploy or return

  request do
    qs = []
    qs << "environment=#{environment}"           if environment
    qs << "message=#{message}"                   if message
    qs << "github_url=#{URI.escape(github_url)}" if github_url
    qs << "scm_version=#{scm_version}"           if scm_version

    uri = "#{http_uri}/deploy"
    uri = "#{uri}?#{qs.join('&')}" unless qs.blank?
    uri = URI.escape(uri)

    packfile = build_packfile(opts)
    response = connection.put(uri, packfile, packfile.headers)
    callback.deploy_complete if callback
    Kernel.system(opts[:post_deploy]) if opts[:post_deploy]
    response
  end

  environment_url(environment)
end

#environment_url(environment) ⇒ Object


45
46
47
48
49
50
51
52
# File 'lib/strobe/resources/application.rb', line 45

def environment_url(environment)
  if environment.blank? || environment == "production"
    web_url
  else
    uri = web_install['environment_uri']
    "#{environment}.#{uri}"
  end
end

#idObject


24
25
26
# File 'lib/strobe/resources/application.rb', line 24

def id
  self[:id]
end

#rollback!(sha, opts = {}) ⇒ Object


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
# File 'lib/strobe/resources/application.rb', line 61

def rollback!(sha, opts = {})
  environment = opts[:environment]
  message     = opts[:message]

  id = self[:id]

  request do
    qs = []
    qs << "message=#{message}"          if message
    qs << "application_id=#{id}"

    uri = URI.escape("/deploys?#{qs.join('&')}")

    body = { :deploy => sha }
    body[:environment] = environment if environment

    headers = {
      "Content-Type" => "application/json",
      "Accept"       => "application/json"
    }

    response = connection.post(uri, body, headers)
    response
  end

  environment_url(environment)
end

#set_web_url!(url) ⇒ Object


54
55
56
57
58
59
# File 'lib/strobe/resources/application.rb', line 54

def set_web_url!(url)
  return if url.blank?
  install = web_install
  install['url'] = install['install_uri'] = url
  install.save!
end

#web_installObject


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/strobe/resources/application.rb', line 28

def web_install
  @web_install ||= begin
    install = nil

    retry_on_error([IOError, Timeout::Error]) do
      install = self.platform_installs.detect { |p| p.web? }
    end

    raise "Application does not have web platform" unless install
    install
  end
end

#web_urlObject


41
42
43
# File 'lib/strobe/resources/application.rb', line 41

def web_url
  web_install['install_uri']
end