Class: Avm::Stereotypes::EacWebappBase0::Deploy

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/avm/stereotypes/eac_webapp_base0/deploy.rb,
lib/avm/stereotypes/eac_webapp_base0/deploy/file_unit.rb,
lib/avm/stereotypes/eac_webapp_base0/deploy/_appended_directories.rb

Defined Under Namespace

Classes: FilesUnit

Constant Summary collapse

DEFAULT_REFERENCE =
'HEAD'
JOBS =
%w[git_deploy setup_files_units assert_instance_branch request_test].freeze
APPENDED_DIRECTORIES_ENTRY_KEY =
'deploy.appended_directories'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, options = {}) ⇒ Deploy

Returns a new instance of Deploy.



29
30
31
32
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 29

def initialize(instance, options = {})
  @instance = instance
  @options = options
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



27
28
29
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 27

def instance
  @instance
end

#optionsObject (readonly)

Returns the value of attribute options.



27
28
29
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 27

def options
  @options
end

Instance Method Details

#appended_directoriesObject



11
12
13
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy/_appended_directories.rb', line 11

def appended_directories
  appended_directories_from_instance_entry + appended_directories_from_options
end

#appended_directories_from_instance_entryObject



15
16
17
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy/_appended_directories.rb', line 15

def appended_directories_from_instance_entry
  ::Avm::PathString.paths(instance.read_entry_optional(APPENDED_DIRECTORIES_ENTRY_KEY))
end

#appended_directories_from_optionsObject



19
20
21
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy/_appended_directories.rb', line 19

def appended_directories_from_options
  options[:appended_directories] || []
end

#assert_instance_branchObject



79
80
81
82
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 79

def assert_instance_branch
  infom 'Setting instance branch...'
  git.execute!('push', git_remote_name, "#{commit_sha1}:refs/heads/#{instance.id}", '-f')
end

#build_git_commitObject



34
35
36
37
38
39
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 34

def build_git_commit
  ::Avm::Git::Commit.new(git, commit_sha1).deploy_to_env_path(
    instance.host_env,
    instance.read_entry(:fs_path)
  ).variables_source_set(instance)
end

#commit_sha1_uncachedObject



92
93
94
95
96
97
98
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 92

def commit_sha1_uncached
  git_fetch
  r = git.rev_parse(git_reference_found)
  return r if r

  raise ::Avm::Result::Error, "No commit SHA1 found for \"#{git_reference_found}\""
end

#git_deployObject



62
63
64
65
66
67
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 62

def git_deploy
  infom 'Deploying source code and appended content...'
  build_git_commit.append_directory(template.path).append_directories(
    appended_directories
  ).run
end

#git_fetch_uncachedObject



128
129
130
131
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 128

def git_fetch_uncached
  infom "Fetching remote \"#{git_remote_name}\" from \"#{git_repository_path}\"..."
  git.fetch(git_remote_name)
end

#git_referenceObject



69
70
71
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 69

def git_reference
  options[:reference] || DEFAULT_REFERENCE
end

#git_reference_found_uncachedObject



100
101
102
103
104
105
106
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 100

def git_reference_found_uncached
  %w[git_reference instance_branch master_branch].map { |b| send(b) }.find(&:present?) ||
    raise(
      ::Avm::Result::Error,
      'No git reference found (Searched for option, instance and master)'
    )
end

#git_remote_hashs_uncachedObject



124
125
126
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 124

def git_remote_hashs_uncached
  git.remote_hashs(git_remote_name)
end

#git_remote_nameObject



120
121
122
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 120

def git_remote_name
  ::Avm::Git::DEFAULT_REMOTE_NAME
end

#git_repository_pathObject



133
134
135
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 133

def git_repository_path
  instance.source_instance.read_entry(:fs_path)
end

#git_uncachedObject



108
109
110
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 108

def git_uncached
  ::EacLauncher::Git::Base.new(git_repository_path)
end

#instance_branchObject



112
113
114
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 112

def instance_branch
  remote_branch(instance.id)
end

#master_branchObject



116
117
118
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 116

def master_branch
  remote_branch('master')
end

#remote_branch(name) ⇒ Object



137
138
139
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 137

def remote_branch(name)
  git_remote_hashs.key?("refs/heads/#{name}") ? "#{git_remote_name}/#{name}" : nil
end

#request_testObject



84
85
86
87
88
89
90
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 84

def request_test
  infom 'Requesting web interface...'
  uri = URI(instance.read_entry('web.url'))
  response = ::Net::HTTP.get_response(uri)
  infov 'Response status', response.code
  fatal_error "Request to #{uri} failed" unless response.code.to_i == 200
end

#runObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 41

def run
  start_banner
  JOBS.each do |job|
    run_callbacks job do
      send(job)
    end
  end
  ::Avm::Result.success('Deployed')
rescue ::Avm::Result::Error => e
  e.to_result
end

#setup_files_unitsObject



73
74
75
76
77
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 73

def setup_files_units
  instance.class.const_get('FILES_UNITS').each do |data_key, fs_path_subpath|
    FilesUnit.new(self, data_key, fs_path_subpath).run
  end
end

#start_bannerObject



53
54
55
56
57
58
59
60
# File 'lib/avm/stereotypes/eac_webapp_base0/deploy.rb', line 53

def start_banner
  infov 'Instance', instance
  infov 'Git reference (User)', git_reference.if_present('- BLANK -')
  infov 'Git remote name', git_remote_name
  infov 'Git reference (Found)', git_reference_found
  infov 'Git commit SHA1', commit_sha1
  infov 'Appended directories', appended_directories
end