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
CA_CERT_PATH =
File.join(EXT_PATH, "../resources/cacert.pem").freeze

Instance Method Summary collapse

Instance Method Details

#abort_installation(reason) ⇒ Object



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

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

#check_architectureObject



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

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



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

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

#download_archive(arch_config, type) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'ext/base.rb', line 105

def download_archive(arch_config, type)
  report["build"]["source"] = "remote"
  if arch_config.key?(type)
    download_url = arch_config[type]["download_url"]
    report["download"]["download_url"] = download_url
    open(download_url, :ssl_ca_cert => CA_CERT_PATH)
  else
    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"
    )
  end
end

#ext_path(path) ⇒ Object



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

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

#fail_installation_with_error(error) ⇒ Object



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

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



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

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
55
# 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
  arch_config = AGENT_CONFIG["triples"][ARCH]

  if local_build?
    report["build"]["source"] = "local"
  else
    archive = download_archive(arch_config, library_type)
    return unless archive
    return unless verify_archive(archive, arch_config, 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)


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

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.



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

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



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

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(__dir__),
          "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



148
149
150
151
# File 'ext/base.rb', line 148

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



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

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

#unarchive(archive) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'ext/base.rb', line 134

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, arch_config, type) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'ext/base.rb', line 121

def verify_archive(archive, arch_config, 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



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

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