Class: Biosphere::CLI::RenameDeployment

Inherits:
Object
  • Object
show all
Defined in:
lib/biosphere/cli/renamedeployment.rb

Class Method Summary collapse

Class Method Details

.renamedeployment(suite, s3, build_dir, deployment, new_name, terraform: Biosphere::CLI::TerraformUtils.new(), localmode: false, force: false) ⇒ Object



11
12
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/biosphere/cli/renamedeployment.rb', line 11

def self.renamedeployment(suite, s3, build_dir, deployment, new_name, terraform: Biosphere::CLI::TerraformUtils.new(), localmode: false, force: false)

    if !suite.kind_of?(::Biosphere::Suite)
        raise ArgumentError, "RenameDeployment needs a proper suite as the first argument"
    end

    if !s3.kind_of?(S3)
        raise ArgumentError, "RenameDeployment requires an s3 client as the second argument"
    end

    localmode = suite.biosphere_settings[:local] || localmode

    if !deployment
        puts "Please specify deployment name as the second parameter."
        puts "Available deployments:"
        suite.deployments.each do |name, deployment|
            puts "\t#{name}"
        end
        exit(-1)
    end

    if !new_name
        puts "Please specify the new name as the third parameter."
        exit(-1)
    end

    if !suite.deployments[deployment]
        puts "Deployment #{deployment} doesn't exist in the current suite".colorize(:red)
        exit(-1)
    elsif suite.deployments[new_name]
        puts "The current suite already contains a deployment called #{new_name}".colorize(:red)
        exit(-1)
    end

    puts "Renaming #{deployment} to #{new_name}"

    suite.deployments[deployment].all_resources.each do |r|
        r[:new_name] = r[:name].sub(deployment,new_name)
    end

    state_file = "#{build_dir}/#{deployment}.tfstate"
    suite.deployments[deployment].all_resources.each do |r|
        terraform.move(state_file, r[:type], r[:name], r[:new_name])
        r[:name] = r[:new_name]
        r.delete(:new_name)
    end
    suite.state.node[:deployments][new_name] = suite.state.node[:deployments].delete(deployment)

    puts "State renaming done!".colorize(:green)
    puts "Remember to change the deployment name in the deployment specifications too!".colorize(:yellow)

    count = 0
    suite.write_json_to(build_dir) do |file_name, destination, str, suite_deployment|
        puts "Wrote #{str.length} bytes from #{file_name} to #{destination} (#{suite_deployment.export["resource"].length} resources)"
        count = count + 1
    end

    puts "Wrote #{count} files under #{build_dir}"

    unless suite.state.node[:biosphere].nil?
        suite.state.node[:biosphere][:last_build_time] = Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
    end

    FileUtils.mv("#{build_dir}/#{deployment}.tfstate", "#{build_dir}/#{new_name}.tfstate")
    suite.state.save()
    s3.save("#{build_dir}/state.node") unless localmode
    s3.delete_object("#{build_dir}/#{deployment}.tfstate") unless localmode
    s3.save("#{build_dir}/#{new_name}.tfstate") unless localmode
end