Class: HostSyncManager

Inherits:
Object
  • Object
show all
Defined in:
lib/HostSyncManager.rb

Overview

This helper module introduces a common routine that synchronizes the “remotes”.

Instance Method Summary collapse

Constructor Details

#initialize(one_config = nil) ⇒ HostSyncManager

Returns a new instance of HostSyncManager.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/HostSyncManager.rb', line 32

def initialize(one_config = nil)
    one_location = ENV['ONE_LOCATION']&.delete("'")
    if one_location.nil?
        @one_config_path         = '/var/lib/one/config'
        @local_scripts_base_path = '/var/lib/one/remotes'
    else
        @one_config_path         = one_location + '/var/config'
        @local_scripts_base_path = one_location + '/var/remotes'
    end

    # Do a simple parsing of the config file unless the values
    # are already provided. NOTE: We don't care about "arrays" here..
    one_config ||= File.read(@one_config_path).lines.each_with_object({}) \
    do |line, object|
        key, value = line.split('=').map(&:strip)
        object[key.upcase] = value
    end

    @remote_scripts_base_path = one_config['SCRIPTS_REMOTE_DIR']
    @remote_scripts_base_path&.delete!("'")
end

Instance Method Details

#error?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
# File 'lib/HostSyncManager.rb', line 103

def error?(cmd)
    return false if cmd.code == 0

    STDERR.puts cmd.stderr
    STDOUT.puts cmd.stdout
    true
end

#update_remotes(hostname, logger = nil, copy_method = :rsync, subset = nil) ⇒ Object



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/HostSyncManager.rb', line 54

def update_remotes(hostname, logger = nil, copy_method = :rsync, subset = nil)
    sources = '.'

    if subset && copy_method == :rsync
        # Make sure all files in the subset exist (and are relative).
        subset.each do |path|
            File.realpath path, @local_scripts_base_path
        end

        sources = subset.join(' ')
    end

    assemble_cmd = lambda do |steps|
        "exec 2>/dev/null; #{steps.join(' && ')}"
    end

    case copy_method
    when :ssh
        mkdir_cmd = assemble_cmd.call [
            "rm -rf '#{@remote_scripts_base_path}'/",
            "mkdir -p '#{@remote_scripts_base_path}'/"
        ]

        sync_cmd = assemble_cmd.call [
            "cd '#{@local_scripts_base_path}'/",
            "scp -rp #{sources} " \
                "'#{hostname}':'#{@remote_scripts_base_path}'/"
        ]
    when :rsync
        mkdir_cmd = assemble_cmd.call [
            "mkdir -p '#{@remote_scripts_base_path}'/"
        ]

        sync_cmd = assemble_cmd.call [
            "cd '#{@local_scripts_base_path}'/",
            "rsync -LRaz --delete #{sources} " \
                "'#{hostname}':'#{@remote_scripts_base_path}'/"
        ]
    end

    cmd = SSHCommand.run(mkdir_cmd, hostname, logger)
    return cmd.code if error?(cmd)

    cmd = LocalCommand.run(sync_cmd, logger)
    return cmd.code if error?(cmd)

    0
end