Top Level Namespace

Defined Under Namespace

Modules: Appsignal Classes: Object

Constant Summary collapse

EXT_PATH =
File.expand_path("..", __FILE__).freeze
AGENT_CONFIG =
YAML.load(File.read(File.join(EXT_PATH, "agent.yml"))).freeze
AGENT_PLATFORM =
Appsignal::System.agent_platform
ARCH =
"#{RbConfig::CONFIG["host_cpu"]}-#{AGENT_PLATFORM}".freeze
ARCH_CONFIG =
AGENT_CONFIG["triples"][ARCH].freeze
CA_CERT_PATH =
File.join(EXT_PATH, "../resources/cacert.pem").freeze

Instance Method Summary collapse

Instance Method Details

#abort_installation(reason) ⇒ Object



74
75
76
77
78
79
80
# File 'ext/base.rb', line 74

def abort_installation(reason)
  report["result"] = {
    "status" => "failed",
    "message" => reason
  }
  false
end

#check_architectureObject



95
96
97
98
99
100
101
102
103
104
# File 'ext/base.rb', line 95

def check_architecture
  if AGENT_CONFIG["triples"].key?(ARCH)
    true
  else
    abort_installation(
      "AppSignal currently does not support your system architecture (#{ARCH})." \
        "Please let us know at [email protected], we aim to support everything our customers run."
    )
  end
end

#create_dummy_makefileObject



64
65
66
67
68
# File 'ext/base.rb', line 64

def create_dummy_makefile
  File.open(File.join(EXT_PATH, "Makefile"), "w") do |file|
    file.write "default:\nclean:\ninstall:"
  end
end

#download_archive(type) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'ext/base.rb', line 106

def download_archive(type)
  report["build"]["source"] = "remote"

  unless ARCH_CONFIG.key?(type)
    abort_installation(
      "AppSignal currently does not support your system. " \
        "Expected config for architecture '#{arch}' and package type '#{type}', but none found. " \
        "For a full list of supported systems visit: " \
        "https://docs.appsignal.com/support/operating-systems.html"
    )
    return
  end

  version = AGENT_CONFIG["version"]
  filename = ARCH_CONFIG[type]["filename"]
  attempted_mirror_urls = []

  AGENT_CONFIG["mirrors"].each do |mirror|
    download_url = [mirror, version, filename].join("/")
    attempted_mirror_urls << download_url
    report["download"]["download_url"] = download_url

    begin
      return open(download_url, :ssl_ca_cert => CA_CERT_PATH)
    rescue
      next
    end
  end

  attempted_mirror_urls_mapped = attempted_mirror_urls.map { |mirror| "- #{mirror}" }
  abort_installation(
    "Could not download archive from any of our mirrors. " \
      "Attempted to download the archive from the following urls:\n" \
      "#{attempted_mirror_urls_mapped.join("\n")}\n" \
      "Please make sure your network allows access to any of these mirrors."
  )
end

#ext_path(path) ⇒ Object



18
19
20
# File 'ext/base.rb', line 18

def ext_path(path)
  File.join(EXT_PATH, path)
end

#fail_installation_with_error(error) ⇒ Object



82
83
84
85
86
87
88
89
# File 'ext/base.rb', line 82

def fail_installation_with_error(error)
  report["result"] = {
    "status" => "error",
    "error" => "#{error.class}: #{error}",
    "backtrace" => error.backtrace
  }
  false
end

#have_required_function(library, func) ⇒ Object

rubocop:disable Naming/PredicateName



75
76
77
78
79
80
81
82
83
84
85
86
# File 'ext/extconf.rb', line 75

def have_required_function(library, func) # rubocop:disable Naming/PredicateName
  if have_func(func)
    report["build"]["dependencies"][library] = "linked"
    return
  end

  report["build"]["dependencies"][library] = "not linked"
  abort_installation("Missing function '#{func}'")
  # Exit with true/0/success because the AppSignal installation should never
  # break a build
  exit
end

#installObject



9
10
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
# File 'ext/extconf.rb', line 9

def install
  library_type = "static"
  report["language"]["implementation"] = "ruby"
  report["build"]["library_type"] = library_type
  return unless check_architecture

  if local_build?
    report["build"]["source"] = "local"
  else
    archive = download_archive(library_type)
    return unless archive
    return unless verify_archive(archive, library_type)
    unarchive(archive)
  end

  is_linux_system = [
    Appsignal::System::LINUX_TARGET,
    Appsignal::System::MUSL_TARGET
  ].include?(AGENT_PLATFORM)

  require "mkmf"
  link_libraries if is_linux_system

  if !have_library("appsignal", "appsignal_start", "appsignal.h")
    abort_installation("Library libappsignal.a or appsignal.h not found")
  elsif !find_executable("appsignal-agent", EXT_PATH)
    abort_installation("File appsignal-agent not found")
  else
    if is_linux_system
      # Statically link libgcc and libgcc_s libraries.
      # Dependencies of the libappsignal extension library.
      # If the gem is installed on a host with build tools installed, but is
      # run on one that isn't the missing libraries will cause the extension
      # to fail on start.
      $LDFLAGS += " -static-libgcc" # rubocop:disable Style/GlobalVars
      report["build"]["flags"]["LDFLAGS"] = $LDFLAGS # rubocop:disable Style/GlobalVars
    end
    create_makefile "appsignal_extension"
    successful_installation
  end
rescue => error
  fail_installation_with_error(error)
ensure
  create_dummy_makefile unless installation_succeeded?
  write_report
end

#installation_succeeded?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'ext/base.rb', line 91

def installation_succeeded?
  report["result"]["status"] == "success"
end

Ruby 2.6 requires us to statically link more libraries we use in our extension library than previous versions. Needed for normal Linux libc and musl builds.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'ext/extconf.rb', line 59

def link_libraries
  if RbConfig::CONFIG["THREAD_MODEL"] == "pthread"
    # Link gem extension against pthread library
    have_library "pthread"
    have_required_function "pthread", "pthread_create"
  end

  # Links gem extension against the `dl` library. This is needed when Ruby is
  # not linked against `dl` itself, so link it on the gem extension.
  have_library "dl"
  # Check if functions are available now from the linked library
  %w[dlopen dlclose dlsym].each do |func|
    have_required_function "dl", func
  end
end

#local_build?Boolean

Returns:

  • (Boolean)


3
4
5
6
7
# File 'ext/extconf.rb', line 3

def local_build?
  File.exist?(ext_path("appsignal-agent")) &&
    File.exist?(ext_path("libappsignal.a")) &&
    File.exist?(ext_path("appsignal.h"))
end

#reportObject



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
# File 'ext/base.rb', line 22

def report
  @report ||=
    begin
      rbconfig = RbConfig::CONFIG
      {
        "result" => {
          "status" => "incomplete"
        },
        "language" => {
          "name" => "ruby",
          "version" => "#{rbconfig["ruby_version"]}-p#{rbconfig["PATCHLEVEL"]}"
        },
        "download" => {
          "checksum" => "unverified"
        },
        "build" => {
          "time" => Time.now.utc,
          "package_path" => File.dirname(EXT_PATH),
          "architecture" => rbconfig["host_cpu"],
          "target" => AGENT_PLATFORM,
          "musl_override" => Appsignal::System.force_musl_build?,
          "dependencies" => {},
          "flags" => {}
        },
        "host" => {
          "root_user" => Process.uid.zero?,
          "dependencies" => {}.tap do |d|
            ldd_output = Appsignal::System.ldd_version_output
            ldd_version = Appsignal::System.extract_ldd_version(ldd_output)
            d["libc"] = ldd_version if ldd_version
          end
        }
      }
    end
end

#store_download_version_on_reportObject



171
172
173
174
# File 'ext/base.rb', line 171

def store_download_version_on_report
  path = File.expand_path(File.join(File.dirname(__FILE__), "appsignal.version"))
  report["build"]["agent_version"] = File.read(path).strip
end

#successful_installationObject



70
71
72
# File 'ext/base.rb', line 70

def successful_installation
  report["result"] = { "status" => "success" }
end

#unarchive(archive) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'ext/base.rb', line 157

def unarchive(archive)
  Gem::Package::TarReader.new(Zlib::GzipReader.open(archive)) do |tar|
    tar.each do |entry|
      next unless entry.file?

      File.open(ext_path(entry.full_name), "wb") do |f|
        f.write(entry.read)
      end
    end
  end
  store_download_version_on_report
  FileUtils.chmod(0o755, ext_path("appsignal-agent"))
end

#verify_archive(archive, type) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
# File 'ext/base.rb', line 144

def verify_archive(archive, type)
  if Digest::SHA256.hexdigest(archive.read) == ARCH_CONFIG[type]["checksum"]
    report["download"]["checksum"] = "verified"
    true
  else
    report["download"]["checksum"] = "invalid"
    abort_installation(
      "Checksum of downloaded archive could not be verified: " \
        "Expected '#{ARCH_CONFIG[type]["checksum"]}', got '#{checksum}'."
    )
  end
end

#write_reportObject



58
59
60
61
62
# File 'ext/base.rb', line 58

def write_report
  File.open(File.join(EXT_PATH, "install.report"), "w") do |file|
    file.write YAML.dump(report)
  end
end