Class: HSDeploy::Target::HostingStack

Inherits:
HSDeploy::Target show all
Defined in:
lib/hsdeploy/target/hostingstack.rb,
lib/hsdeploy/target/hostingstack/api_client.rb

Defined Under Namespace

Classes: ApiClient, ApiError

Constant Summary collapse

SUPPORTED_API_VERSION =
4

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HSDeploy::Target

find, from_config, get_json_resource

Constructor Details

#initialize(options) ⇒ HostingStack

Returns a new instance of HostingStack.



45
46
47
48
49
# File 'lib/hsdeploy/target/hostingstack.rb', line 45

def initialize(options)
  @api_server = options['api_server']
  auth = options.has_key?('refresh_token') ? {:refresh_token => options['refresh_token']} : {:email => options['email'], :password => options['password']}
  @api_client = ApiClient.new(options['api_server'], options['app_name'], auth)
end

Class Method Details

.check_version(api_server) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hsdeploy/target/hostingstack.rb', line 51

def self.check_version(api_server)
  begin
    info = get_json_resource("http://%s/info" % api_server)
  rescue => e
    $logger.debug "Exception: %s\n%s" % [e.message, e.backtrace.join("\n")]
    return false
  end
  return false unless info && info['name'] == "hs"

  if info['api_version'] > SUPPORTED_API_VERSION
    $logger.error "This version of deploytool is outdated.\nThis server requires at least API Version #{info['api_version']}."
    return false
  end
  @cloud_name = info['cloud_name']
  return true
end

.cloud_nameObject



5
6
7
# File 'lib/hsdeploy/target/hostingstack.rb', line 5

def self.cloud_name
  @cloud_name || 'HostingStack'
end

.create(target_spec) ⇒ Object



68
69
70
71
# File 'lib/hsdeploy/target/hostingstack.rb', line 68

def self.create(target_spec)
  app_name, api_server = parse_target_spec(target_spec)
  HostingStack.new('api_server' => api_server, 'app_name' => app_name)
end

.matches?(target_spec) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/hsdeploy/target/hostingstack.rb', line 28

def self.matches?(target_spec)
  return true if parse_target_spec(target_spec)
end

.parse_target_spec(target_spec) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hsdeploy/target/hostingstack.rb', line 13

def self.parse_target_spec(target_spec)
  server, app_name = target_spec.split('@').reverse
  if app_name.nil?
    app_name = server.split('.', 2).first
  end
  [server, 'api.' + server, 'api.' + server.split('.', 2).last].each do |api_server|
    begin
      return [app_name, api_server] if check_version(api_server)
    rescue => e
      puts e
    end
  end
  nil
end

.support_emailObject



9
10
11
# File 'lib/hsdeploy/target/hostingstack.rb', line 9

def self.support_email
  @support_email || '[email protected]'
end

Instance Method Details

#exec(opts) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/hsdeploy/target/hostingstack.rb', line 119

def exec(opts)
  self.class.check_version(@api_server)
  info = @api_client.info
  if info[:blocking_deployment]
    $logger.error info[:blocking_deployment]
    exit 4
  end

  @api_client.exec(opts) # Blocks til done
rescue => e
  if e.message.start_with?("ERROR")
    puts e.message
  else
    $logger.debug e.backtrace.join("\n")
    $logger.info "Unknown error happened: #{e.message}"
  end
end

#push(opts) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hsdeploy/target/hostingstack.rb', line 95

def push(opts)
  self.class.check_version(@api_server)
  info = @api_client.info
  if info[:blocking_deployment]
    $logger.error info[:blocking_deployment]
    exit 4
  end
  if info[:warn_deployment]
    $logger.info info[:warn_deployment]
    exit 5 if HighLine.new.ask("Deploy anyway? (y/N)").downcase.strip != 'y'
  end

  code_token = @api_client.upload
  deploy_token = @api_client.deploy(code_token)
  @api_client.deploy_status(deploy_token, opts) # Blocks till deploy is done
rescue => e
  if e.message.start_with?("ERROR")
    puts e.message
  else
    $logger.debug e.backtrace.join("\n")
    $logger.info "Unknown error happened: #{e.message}. Please try again or contact support."
  end
end

#to_hObject



32
33
34
35
36
37
38
39
# File 'lib/hsdeploy/target/hostingstack.rb', line 32

def to_h
  x = {:type => "HostingStack", :api_server => @api_client.server, :app_name => @api_client.app_name,}
  if @api_client.auth_method == :refresh_token
    x.merge({:refresh_token => @api_client.refresh_token})
  else
    x
  end
end

#to_sObject



41
42
43
# File 'lib/hsdeploy/target/hostingstack.rb', line 41

def to_s
  "%s@%s (HS-based platform)" % [@api_client.app_name, @api_client.server]
end

#verifyObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hsdeploy/target/hostingstack.rb', line 73

def verify
  self.class.check_version(@api_server)
  begin
    info = @api_client.info
    return true
  rescue => e
    $logger.debug "Exception: %s %s\n  %s" % [e.class.name, e.message, e.backtrace.join("\n  ")]
    if e.message.include?("401 ")
      $logger.error "Authentication failed (password wrong?)"
    elsif e.message.include?("404 ")
      $logger.error "Application does not exist"
    elsif e.message.start_with?("ERROR")
      puts e.message
      $logger.info "\nPlease contact %s support and include the above output: %s" % [HostingStack.cloud_name, HostingStack.support_email]
    else
      $logger.error "Remote server said: %s" % [e.message]
      $logger.info "\nPlease contact %s support and include the above output: %s" % [HostingStack.cloud_name, HostingStack.support_email]
    end
  end
  exit 5
end