Class: Renuo::Cli::Commands::ConfigureSemaphore

Inherits:
Object
  • Object
show all
Includes:
CredentialsHelper
Defined in:
lib/renuo/cli/commands/configure_semaphore.rb

Constant Summary

Constants included from CredentialsHelper

CredentialsHelper::CONFIG

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CredentialsHelper

#credentials

Constructor Details

#initializeConfigureSemaphore

Returns a new instance of ConfigureSemaphore.



16
17
18
19
# File 'lib/renuo/cli/commands/configure_semaphore.rb', line 16

def initialize
  @project_name = File.basename(Dir.getwd)
  @slack_endpoint = credentials[:slack_endpoint]
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



6
7
8
# File 'lib/renuo/cli/commands/configure_semaphore.rb', line 6

def environment
  @environment
end

#project_nameObject

Returns the value of attribute project_name.



6
7
8
# File 'lib/renuo/cli/commands/configure_semaphore.rb', line 6

def project_name
  @project_name
end

#slack_endpointObject

Returns the value of attribute slack_endpoint.



6
7
8
# File 'lib/renuo/cli/commands/configure_semaphore.rb', line 6

def slack_endpoint
  @slack_endpoint
end

Instance Method Details

#create_semaphore_deployment_targetsObject



55
56
57
58
# File 'lib/renuo/cli/commands/configure_semaphore.rb', line 55

def create_semaphore_deployment_targets
  system("sem create dt main -p #{project_name}")
  system("sem create dt develop -p #{project_name}")
end

#create_semaphore_notificationObject



45
46
47
48
49
# File 'lib/renuo/cli/commands/configure_semaphore.rb', line 45

def create_semaphore_notification
  File.write("tmp/notification.#{project_name}.yml", render("../templates/semaphore/notification.yml.erb"))
  system("sem create -f tmp/notification.#{project_name}.yml")
  FileUtils.rm("tmp/notification.#{project_name}.yml")
end

#create_semaphore_secretsObject



51
52
53
# File 'lib/renuo/cli/commands/configure_semaphore.rb', line 51

def create_semaphore_secrets
  system("sem create secret -p #{project_name} #{project_name}")
end

#render(template_file) ⇒ Object



60
61
62
63
64
65
# File 'lib/renuo/cli/commands/configure_semaphore.rb', line 60

def render(template_file)
  file_path = File.join(File.dirname(__FILE__), template_file)
  semaphore_template = File.read(file_path)
  renderer = ERB.new(semaphore_template)
  renderer.result(binding)
end

#runObject

rubocop:disable Metrics/MethodLength



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/renuo/cli/commands/configure_semaphore.rb', line 21

def run # rubocop:disable Metrics/MethodLength
  return unless semaphore_cli_installed?

  FileUtils.mkdir_p(%w[.semaphore .semaphore/bin tmp])

  write_or_warn(".semaphore/semaphore.yml", render("../templates/semaphore/semaphore.yml.erb"))
  %w[main develop].each do |environment|
    @environment = environment
    write_or_warn(".semaphore/#{environment}-deploy.yml", render("../templates/semaphore/semaphore-deploy.yml.erb"))
  end
  write_or_warn(".semaphore/bin/cache_restore", render("../templates/semaphore/bin/cache_restore.erb"))
  write_or_warn(".semaphore/bin/cache_store", render("../templates/semaphore/bin/cache_store.erb"))

  create_semaphore_notification
  create_semaphore_secrets
  create_semaphore_deployment_targets
end

#semaphore_cli_installed?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/renuo/cli/commands/configure_semaphore.rb', line 39

def semaphore_cli_installed?
  semaphore_cli_installed = `sem context | grep '*'`.strip == "* renuo_semaphoreci_com"
  warn("You need to install and configure Semaphore CLI to run this command.") unless semaphore_cli_installed
  semaphore_cli_installed
end

#write_or_warn(file_path, content) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/renuo/cli/commands/configure_semaphore.rb', line 67

def write_or_warn(file_path, content)
  if File.exist?(file_path)
    warn("#{file_path} exists already. I will not overwrite it.")
  else
    File.write(file_path, content)
    say("#{file_path} added.")
  end
end