forked from scalyr/scalyr-agent-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_package.py
1702 lines (1404 loc) · 65 KB
/
build_package.py
1
2
3
4
5
6
7
8
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
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
102
103
104
105
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#
# Copyright 2014 Scalyr Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------
#
# Script used to build the RPM, Debian, and tarball packages for releasing Scalyr Agent 2.
#
# To execute this script, you must have installed fpm: https://github.com/jordansissel/fpm
#
# Usage: python build_package.py [options] rpm|tarball|deb
#
# author: Steven Czerwinski <[email protected]>
__author__ = "[email protected]"
import errno
import glob
import os
import re
import shutil
import stat
import subprocess
import sys
import tarfile
import tempfile
import time
import uuid
from cStringIO import StringIO
from optparse import OptionParser
from time import gmtime, strftime
from scalyr_agent.__scalyr__ import get_install_root, SCALYR_VERSION, scalyr_init
scalyr_init()
# The root of the Scalyr repository should just be the parent of this file.
__source_root__ = get_install_root()
# All the different packages that this script can build.
PACKAGE_TYPES = [
"rpm",
"tarball",
"deb",
"win32",
"docker_syslog_builder",
"docker_json_builder",
"k8s_builder",
]
def build_package(package_type, variant, no_versioned_file_name):
"""Builds the scalyr-agent-2 package specified by the arguments.
The package is left in the current working directory. The file name of the
package is returned by this function.
@param package_type: One of `PACKAGE_TYPES`. Determines which package type is built.
@param variant: Adds the specified string into the package's iteration name. This may be None if no additional
tweak to the name is required. This is used to produce different packages even for the same package type (such
as 'rpm').
@param no_versioned_file_name: If True, will not embed a version number in the resulting artifact's file name.
This only has an affect if building one of the tarball formats.
@return: The file name of the produced package.
"""
original_cwd = os.getcwd()
version = SCALYR_VERSION
# Create a temporary directory to build the package in.
tmp_dir = tempfile.mkdtemp(prefix="build-scalyr-agent-packages")
try:
# Change to that directory and delegate to another method for the specific type.
os.chdir(tmp_dir)
if package_type == "tarball":
artifact_file_name = build_tarball_package(
variant, version, no_versioned_file_name
)
elif package_type == "win32":
artifact_file_name = build_win32_installer_package(variant, version)
elif package_type == "docker_syslog_builder":
# An image for running on Docker configured to receive logs from other containers via syslog.
# This is the deprecated approach (but is still published under scalyr/scalyr-docker-agent for
# backward compatibility.) We also publish this under scalyr/scalyr-docker-agent-syslog to help
# with the eventual migration.
artifact_file_name = build_container_builder(
variant,
version,
no_versioned_file_name,
"scalyr-docker-agent.tar.gz",
"docker/Dockerfile.syslog",
"docker/docker-syslog-config",
"scalyr-docker-agent-syslog",
["scalyr/scalyr-agent-docker-syslog", "scalyr/scalyr-agent-docker"],
)
elif package_type == "docker_json_builder":
# An image for running on Docker configured to fetch logs via the file system (the container log
# directory is mounted to the agent container.) This is the preferred way of running on Docker.
# This image is published to scalyr/scalyr-agent-docker-json.
artifact_file_name = build_container_builder(
variant,
version,
no_versioned_file_name,
"scalyr-docker-agent.tar.gz",
"docker/Dockerfile",
"docker/docker-json-config",
"scalyr-docker-agent-json",
["scalyr/scalyr-agent-docker-json"],
)
elif package_type == "k8s_builder":
# An image for running the agent on Kubernetes.
artifact_file_name = build_container_builder(
variant,
version,
no_versioned_file_name,
"scalyr-k8s-agent.tar.gz",
"docker/Dockerfile.k8s",
"docker/k8s-config",
"scalyr-k8s-agent",
["scalyr/scalyr-k8s-agent"],
)
else:
assert package_type in ("deb", "rpm")
artifact_file_name = build_rpm_or_deb_package(
package_type == "rpm", variant, version
)
os.chdir(original_cwd)
# Move the artifact (built package) to the original current working dir.
shutil.move(os.path.join(tmp_dir, artifact_file_name), artifact_file_name)
return artifact_file_name
finally:
# Be sure to delete the temporary directory.
os.chdir(original_cwd)
shutil.rmtree(tmp_dir)
# A GUID representing Scalyr products, used to generate a per-version guid for each version of the Windows
# Scalyr Agent. DO NOT MODIFY THIS VALUE, or already installed software on clients machines will not be able
# to be upgraded.
_scalyr_guid_ = uuid.UUID("{0b52b8a0-22c7-4d50-92c1-8ea3b258984e}")
def build_win32_installer_package(variant, version):
"""Builds an MSI that will install the agent on a win32 machine in the current working directory.
Note, this can only be run on a Windows machine with the proper binaries and packages installed.
@param variant: If not None, will add the specified string to the GUID used to identify the installed
executables. This can be used to avoid customer builds of the agent from colliding with the Scalyr-built
ones.
@param version: The agent version.
@return: The file name of the built package.
"""
if os.getenv("WIX") is None:
print >> sys.stderr, "Error, the WIX toolset does not appear to be installed."
print >> sys.stderr, "Please install it to build the Windows Scalyr Agent installer."
print >> sys.stderr, "See http://wixtoolset.org."
sys.exit(1)
try:
import psutil
except ImportError:
# noinspection PyUnusedLocal
psutil = None
print >> sys.stderr, "Error, the psutil Python module is not installed. This is required to build the"
print >> sys.stderr, "Windows version of the Scalyr Agent. Please download and install it."
print >> sys.stderr, "See http://pythonhosted.org/psutil/"
print >> sys.stderr, 'On many systems, executing "pip install psutil" will install the package.'
sys.exit(1)
make_directory("source_root")
make_directory("data_files")
agent_source_root = __source_root__
# Populate source_root
os.chdir("source_root")
shutil.copytree(make_path(agent_source_root, "scalyr_agent"), "scalyr_agent")
# We have to move __scalyr__.py up to the top of the source_root since, when running in the environment
# generated by py2exe, an 'import __scalyr__.py' will not look in the current directory.. it will only look
# for that module at the top of the sources_root. Essentially, the PYTHONPATH variable only has a single
# entry in it, and it does not have '.' in it. We leave a copy of __scalyr__.py in the original scalyr_agent
# directory because we need it there when we execute setup.py. For the same reason, we put a copy of VERSION.
shutil.copy(convert_path("scalyr_agent/__scalyr__.py"), "__scalyr__.py")
shutil.copy(make_path(agent_source_root, "VERSION"), "VERSION")
shutil.copy(
make_path(agent_source_root, "VERSION"), convert_path("scalyr_agent/VERSION")
)
shutil.copytree(make_path(agent_source_root, "monitors"), "monitors")
os.chdir("monitors")
recursively_delete_files_by_name("README.md")
os.chdir("..")
# Exclude certain files.
# TODO: Should probably use MANIFEST.in to do this, but don't know the Python-fu to do this yet.
#
# Don't include the tests directories. Also, don't include the .idea directory created by IDE.
recursively_delete_dirs_by_name("\.idea", "tests")
recursively_delete_files_by_name(
".*\.pyc", ".*\.pyo", ".*\.pyd", "all_tests\.py", ".*~"
)
# exclude all the third_party_tls libs under windows
# because windows python has tls built in.
recursively_delete_dirs_by_name("third_party_tls")
# Move back up to the root directory and populate the data_files.
os.chdir("..")
os.chdir("data_files")
# Copy the version file. We copy it both to the root and the package root. The package copy is done down below.
shutil.copy(make_path(agent_source_root, "VERSION"), "VERSION")
shutil.copy(make_path(agent_source_root, "LICENSE.txt"), "LICENSE.txt")
# Copy the third party licenses
shutil.copytree(
make_path(agent_source_root, "scalyr_agent/third_party/licenses"), "licenses"
)
# Copy the config file.
cat_files(
[make_path(agent_source_root, "config/agent.json")],
"agent_config.tmpl",
convert_newlines=True,
)
os.chdir("..")
# We need to place a 'setup.py' here so that when we executed py2exe it finds it.
shutil.copy(make_path(agent_source_root, "setup.py"), "setup.py")
shutil.copy(
make_path(agent_source_root, "DESCRIPTION.rst"),
convert_path("source_root/DESCRIPTION.rst"),
)
run_command("python.exe setup.py py2exe", exit_on_fail=True, command_name="py2exe")
make_directory("Scalyr/certs")
make_directory("Scalyr/logs")
make_directory("Scalyr/data")
make_directory("Scalyr/config/agent.d")
os.rename("dist", convert_path("Scalyr/bin"))
shutil.copy(
make_path(agent_source_root, "win32/ScalyrShell.cmd"),
"Scalyr/bin/ScalyrShell.cmd",
)
# Copy the cert files.
# AGENT-283: Certificate validation on windows seems to fail when the intermediate certs are present, skipping them
cat_files(
glob_files(make_path(agent_source_root, "certs/*_root.pem")),
"Scalyr/certs/ca_certs.crt",
convert_newlines=True,
)
# Generate the file used by WIX's candle program.
create_wxs_file(
make_path(agent_source_root, "win32/scalyr_agent.wxs"),
convert_path("Scalyr/bin"),
"scalyr_agent.wxs",
)
# Get ready to run wix. Add in WIX to the PATH variable.
os.environ["PATH"] = "%s;%s\\bin" % (os.getenv("PATH"), os.getenv("WIX"))
if variant is None:
variant = "main"
# Generate a unique identifier used to identify this version of the Scalyr Agent to windows.
product_code = uuid.uuid3(_scalyr_guid_, "ProductID:%s:%s" % (variant, version))
# The upgrade code identifies all families of versions that can be upgraded from one to the other. So, this
# should be a single number for all Scalyr produced ones.
upgrade_code = uuid.uuid3(_scalyr_guid_, "UpgradeCode:%s" % variant)
# For prereleases, we use weird version numbers like 4.0.4.pre5.1 . That does not work for Windows which
# requires X.X.X.X. So, we convert if necessary.
if len(version.split(".")) == 5:
parts = version.split(".")
del parts[3]
version = ".".join(parts)
run_command(
'candle -nologo -out ScalyrAgent.wixobj -dVERSION="%s" -dUPGRADECODE="%s" '
'-dPRODUCTCODE="%s" scalyr_agent.wxs' % (version, upgrade_code, product_code),
exit_on_fail=True,
command_name="candle",
)
installer_name = "ScalyrAgentInstaller-%s.msi" % version
run_command(
"light -nologo -ext WixUtilExtension.dll -ext WixUIExtension -out %s ScalyrAgent.wixobj"
% installer_name,
exit_on_fail=True,
command_name="light",
)
return installer_name
def create_wxs_file(template_path, dist_path, destination_path):
"""Performs a rewrite of the Wix file to replace template-like poritions with information about the
binaries/files in `dist_path`.
This is required so that our Windows installer includes all of the DLLs, Python compiled files, etc that py2exe
produced. This list can change over time and is dependent on the build machine, so we cannot hard code this
list. It must be determined dynamically.
The file is rewrite by expanding the 'templates' found between the '<!-- EXPAND_FROM_BIN' markers. This will
make a copy of the included template, once for each file in the `dist_path`, replacing such variables as
$COMPONENT_ID, $COMPONENT_GUID, $FILE_ID, and $FILE_SOURCE with values calculated on the file's information.
You may also specify a list of files to exclude in `dist_path` from the template expansion. This is used for
well-known files that are already in the Wix file.
Here is an example:
<!-- EXPAND_FROM_BIN EXCLUDE:scalyr-agent-2.exe,scalyr-agent-2-config.exe,ScalyrAgentService.exe -->
<Component Id='$COMPONENT_ID' Guid='$COMPONENT_GUID' >
<File Id='$FILE_ID' DiskId='1' KeyPath='yes' Checksum='yes' Source='$FILE_SOURCE' />
</Component>
<!-- EXPAND_FROM_BIN -->
@param template_path: The file path storing the Wix file to copy/rewrite.
@param dist_path: The path to the directory containing the files that should be included in the template
expansion.
@param destination_path: The file path to write the result
@type template_path: str
@type dist_path: str
@type destination_path: str
"""
# First, calculate all of the per-file information for each file in the distribution directory.
dist_files = []
for dist_file_path in glob.glob("%s/*" % dist_path):
base_file = os.path.basename(dist_file_path)
file_id = base_file.replace(".", "_").replace("-", "_")
entry = {
"BASE": base_file,
"FILE_ID": file_id,
"COMPONENT_GUID": str(uuid.uuid3(_scalyr_guid_, "DistComp%s" % base_file)),
"COMPONENT_ID": "%s_comp" % file_id,
"FILE_SOURCE": dist_file_path,
}
dist_files.append(entry)
# For the sake of easier coding, we read all of the lines of the input file into an array.
f = open(template_path)
try:
template_lines = f.readlines()
finally:
f.close()
# Now go through, looking for the markers, and when we find them, do the replacement.
result = []
while len(template_lines) > 0:
if "<!-- EXPAND_FROM_BIN" in template_lines[0]:
result.extend(expand_template(template_lines, dist_files))
else:
l = template_lines[0]
del template_lines[0]
result.append(l)
# Write the resulting lines out.
f = open(destination_path, "wb")
try:
for line in result:
f.write(line)
finally:
f.close()
def expand_template(input_lines, dist_files):
"""Reads the template starting at the first entry in `input_lines` and generates a copy of it for each
item in `dist_files` that is not excluded.
Used by `create_wxs_file`.
This consumes the lines from the `input_lines` list.
@param input_lines: The list of input lines from the file, with the first beginning a template expansion
(should have the <!-- EXPAND_FROM_BIN pragma in it).
@param dist_files: The list of file entries from the distribution directory. The template should be expanded
once for each entry (unless it was specifically excluded).
@type input_lines: [str]
@type dist_files: [{}]
@return: The list of lines produced by the expansion.
@rtype: [str]
"""
# First, see if there were any files that should be excluded. This will be in the first line, prefaced by
# EXCLUDED and a comma separated list.
match = re.search("EXCLUDE:(\S*)", input_lines[0])
del input_lines[0]
if match is not None:
excluded_files = match.group(1).split(",")
else:
excluded_files = []
# Create a list of just the template. We need to find where it ends in the input lines.
template_lines = []
found_end = False
while len(input_lines) > 0:
l = input_lines[0]
del input_lines[0]
if "<!-- EXPAND_FROM_BIN" in l:
found_end = True
break
else:
template_lines.append(l)
if not found_end:
raise Exception("Did not find termination for EXPAND_FROM_BIN")
result = []
# Do the expansion.
for dist_entry in dist_files:
if dist_entry["BASE"] in excluded_files:
continue
for template_line in template_lines:
line = template_line.replace("$FILE_ID", dist_entry["FILE_ID"])
line = line.replace("$COMPONENT_GUID", dist_entry["COMPONENT_GUID"])
line = line.replace("$COMPONENT_ID", dist_entry["COMPONENT_ID"])
line = line.replace("$FILE_SOURCE", dist_entry["FILE_SOURCE"])
result.append(line)
return result
def build_common_docker_and_package_files(create_initd_link, base_configs=None):
"""Builds the common `root` system used by Debian, RPM, and container source tarballs in the current working
directory.
@param create_initd_link: Whether or not to create the link from initd to the scalyr agent binary.
@param base_configs: The directory (relative to the top of the source tree) that contains the configuration
files to copy (such as the agent.json and agent.d directory). If None, then will use `config`.
@type create_initd_link: bool
@type base_configs: str
"""
original_dir = os.getcwd()
# Create the directory structure for where the RPM/Debian package will place files on the system.
make_directory("root/etc/init.d")
make_directory("root/var/log/scalyr-agent-2")
make_directory("root/var/lib/scalyr-agent-2")
make_directory("root/usr/share")
make_directory("root/usr/sbin")
# Place all of the import source in /usr/share/scalyr-agent-2.
os.chdir("root/usr/share")
build_base_files(base_configs=base_configs)
os.chdir("scalyr-agent-2")
# The build_base_files leaves the config directory in config, but we have to move it to its etc
# location. We just rename it to the right directory.
shutil.move(
convert_path("config"), make_path(original_dir, "root/etc/scalyr-agent-2")
)
os.chdir(original_dir)
# Make sure there is an agent.d directory regardless of the config directory we used.
make_directory("root/etc/scalyr-agent-2/agent.d")
# Create the links to the appropriate commands in /usr/sbin and /etc/init.d/
if create_initd_link:
make_soft_link(
"/usr/share/scalyr-agent-2/bin/scalyr-agent-2",
"root/etc/init.d/scalyr-agent-2",
)
make_soft_link(
"/usr/share/scalyr-agent-2/bin/scalyr-agent-2", "root/usr/sbin/scalyr-agent-2"
)
make_soft_link(
"/usr/share/scalyr-agent-2/bin/scalyr-agent-2-config",
"root/usr/sbin/scalyr-agent-2-config",
)
def build_container_builder(
variant,
version,
no_versioned_file_name,
source_tarball,
dockerfile,
base_configs,
image_name,
image_repos,
):
"""Builds an executable script in the current working directory that will build the container image for the various
Docker and Kubernetes targets. This script embeds all assets it needs in it so it can be a standalone artifact.
The script is based on `docker/scripts/container_builder_base.sh`. See that script for information on it can
be used.
@param variant: If not None, will add the specified string into the final script name. This allows for different
scripts to be built for the same type and same version.
@param version: The agent version.
@param no_versioned_file_name: True if the version number should not be embedded in the script's file name.
@param source_tarball: The filename for the source tarball (including the `.tar.gz` extension) that will
be built and then embedded in the artifact. The contents of the Dockerfile will determine what this
name should be.
@param dockerfile: The file path for the Dockerfile to embed in the script, relative to the top of the
agent source directory.
@param base_configs: The file path for the configuration to use when building the container image, relative
to the top of the agent source directory. This allows for different `agent.json` and `agent.d` directories
to be used for Kubernetes, docker, etc.
@param image_name: The name for the image that is being built. Will be used for the artifact's name.
@param image_repos: A list of repositories that should be added as tags to the image once it is built.
Each repository will have two tags added -- one for the specific agent version and one for `latest`.
@return: The file name of the built artifact.
"""
build_container_tarball(source_tarball, base_configs=base_configs)
agent_source_root = __source_root__
# Make a copy of the right Dockerfile to embed in the script.
shutil.copy(make_path(agent_source_root, dockerfile), "Dockerfile")
if variant is None:
version_string = version
else:
version_string = "%s.%s" % (version, variant)
# Read the base builder script into memory
base_fp = open(
make_path(agent_source_root, "docker/scripts/container_builder_base.sh"), "r"
)
base_script = base_fp.read()
base_fp.close()
# The script has two lines defining environment variables (REPOSITORIES and TAGS) that we need to overwrite to
# set them to what we want. We'll just do some regex replace to do that.
base_script = re.sub(
r"\n.*OVERRIDE_REPOSITORIES.*\n",
'\nREPOSITORIES="%s"\n' % ",".join(image_repos),
base_script,
)
base_script = re.sub(
r"\n.*OVERRIDE_TAGS.*\n",
'\nTAGS="%s"\n' % "%s,latest" % version_string,
base_script,
)
if no_versioned_file_name:
output_name = image_name
else:
output_name = "%s-%s" % (image_name, version_string)
# Tar it up but hold the tarfile in memory. Note, if the source tarball really becomes massive, might have to
# rethink this.
tar_out = StringIO()
tar = tarfile.open("assets.tar.gz", "w|gz", tar_out)
tar.add("Dockerfile")
tar.add(source_tarball)
tar.close()
# Write one file that has the contents of the script followed by the contents of the tarfile.
builder_fp = open(output_name, "w")
builder_fp.write(base_script)
builder_fp.write(tar_out.getvalue())
builder_fp.close()
# Make the script executable.
st = os.stat(output_name)
os.chmod(output_name, st.st_mode | stat.S_IEXEC | stat.S_IXGRP)
return output_name
def build_container_tarball(tarball_name, base_configs=None):
"""Builds the scalyr-agent-2 tarball for either Docker or Kubernetes in the current working directory.
@param tarball_name: The name for the output tarball (including the `.tar.gz` extension)
@param base_configs: The directory (relative to the top of the source tree) that contains the configuration
files to copy (such as the agent.json and agent.d directory). If None, then will use `config`.
@type tarball_name: str
@type base_configs: str
@return: The file name of the built tarball.
"""
build_common_docker_and_package_files(False, base_configs=base_configs)
# Need to create some docker specific files
make_directory("root/var/log/scalyr-agent-2/containers")
# Tar it up.
tar = tarfile.open(tarball_name, "w:gz")
original_dir = os.getcwd()
os.chdir("root")
# Do a manual walk over the contents of root so that we can use `addfile` to add the tarfile... which allows
# us to reset the owner/group to root. This might not be that portable to Windows, but for now, Docker is mainly
# Posix.
for root, dirs, files in os.walk("."):
to_copy = []
for name in dirs:
to_copy.append(os.path.join(root, name))
for name in files:
to_copy.append(os.path.join(root, name))
for x in to_copy:
file_entry = tar.gettarinfo(x)
file_entry.uname = "root"
file_entry.gname = "root"
file_entry.uid = 0
file_entry.gid = 0
if file_entry.isreg():
fp = open(file_entry.name, "rb")
tar.addfile(file_entry, fp)
fp.close()
else:
tar.addfile(file_entry)
os.chdir(original_dir)
tar.close()
return tarball_name
def build_rpm_or_deb_package(is_rpm, variant, version):
"""Builds either an RPM or Debian package in the current working directory.
@param is_rpm: True if an RPM should be built. Otherwise a Debian package will be built.
@param variant: If not None, will add the specified string into the iteration identifier for the package. This
allows for different packages to be built for the same type and same version.
@param version: The agent version.
@return: The file name of the built package.
"""
build_common_docker_and_package_files(True)
# Create the scriplets the RPM/Debian package invokes when uninstalling or upgrading.
create_scriptlets()
# Produce the change logs that we will embed in the package, based on the CHANGELOG.md in this directory.
create_change_logs()
if is_rpm:
package_type = "rpm"
else:
package_type = "deb"
# Only change the iteration label if we need to embed a variant.
if variant is not None:
iteration_arg = "--iteration 1.%s" % variant
else:
iteration_arg = ""
description = (
"Scalyr Agent 2 is the daemon process Scalyr customers run on their servers to collect metrics and "
"log files and transmit them to Scalyr."
)
run_command(
'fpm -s dir -a all -t %s -n "scalyr-agent-2" -v %s '
' --license "Apache 2.0" '
" --vendor Scalyr %s "
" --maintainer [email protected] "
" --provides scalyr-agent-2 "
' --description "%s" '
' --depends "python >= 2.4" '
' --depends "bash >= 3.2" '
" --url https://www.scalyr.com "
" --deb-user root "
" --deb-group root "
" --deb-changelog changelog-deb "
" --rpm-user root "
" --rpm-group root "
" --rpm-changelog changelog-rpm"
" --before-install preinstall.sh "
" --after-install postinstall.sh "
" --before-remove preuninstall.sh "
" --config-files /etc/scalyr-agent-2/agent.json "
" --directories /usr/share/scalyr-agent-2 "
" --directories /var/lib/scalyr-agent-2 "
" --directories /var/log/scalyr-agent-2 "
" -C root usr etc var" % (package_type, version, iteration_arg, description),
exit_on_fail=True,
command_name="fpm",
)
# We determine the artifact name in a little bit of loose fashion.. we just glob over the current
# directory looking for something either ending in .rpm or .deb. There should only be one package,
# so that is fine.
if is_rpm:
files = glob.glob("*.rpm")
else:
files = glob.glob("*.deb")
if len(files) != 1:
raise Exception(
"Could not find resulting rpm or debian package in the build directory."
)
return files[0]
def build_tarball_package(variant, version, no_versioned_file_name):
"""Builds the scalyr-agent-2 tarball in the current working directory.
@param variant: If not None, will add the specified string into the final tarball name. This allows for different
tarballs to be built for the same type and same version.
@param version: The agent version.
@param no_versioned_file_name: True if the version number should not be embedded in the artifact's file name.
@return: The file name of the built tarball.
"""
# Use build_base_files to build all of the important stuff in ./scalyr-agent-2
build_base_files()
# Build the rest of the directories required for the tarball install. Mainly, the log and data directories
# in the tarball itself where the running process will store its state.
make_directory("scalyr-agent-2/data")
make_directory("scalyr-agent-2/log")
make_directory("scalyr-agent-2/config/agent.d")
# Create a file named packageless. This signals to the agent that
# this a tarball install instead of an RPM/Debian install, which changes
# the default paths for th econfig, logs, data, etc directories. See
# configuration.py.
write_to_file("1", "scalyr-agent-2/packageless")
if variant is None:
base_archive_name = "scalyr-agent-%s" % version
else:
base_archive_name = "scalyr-agent-%s.%s" % (version, variant)
shutil.move("scalyr-agent-2", base_archive_name)
output_name = (
"%s.tar.gz" % base_archive_name
if not no_versioned_file_name
else "scalyr-agent.tar.gz"
)
# Tar it up.
tar = tarfile.open(output_name, "w:gz")
tar.add(base_archive_name)
tar.close()
return output_name
def build_base_files(base_configs="config"):
"""Build the basic structure for a package in a new directory scalyr-agent-2 in the current working directory.
This creates scalyr-agent-2 in the current working directory and then populates it with the basic structure
required by most of the packages.
It copies the source files, the certs, the configuration directories, etc. This will make sure to exclude
files like .pyc, .pyo, etc.
In the end, the structure will look like:
scalyr-agent-2:
py/scalyr_agent/ -- All the scalyr_agent source files
certs/ca_certs.pem -- The trusted SSL CA root list.
config/agent.json -- The configuration file.
bin/scalyr-agent-2 -- Symlink to the agent_main.py file to run the agent.
bin/scalyr-agent-2-config -- Symlink to config_main.py to run the configuration tool
build_info -- A file containing the commit id of the latest commit included in this package,
the time it was built, and other information.
@param base_configs: The directory (relative to the top of the source tree) that contains the configuration
files to copy (such as the agent.json and agent.d directory). If None, then will use `config`.
"""
original_dir = os.getcwd()
# This will return the parent directory of this file. We will use that to determine the path
# to files like scalyr_agent/ to copy the source files
agent_source_root = __source_root__
make_directory("scalyr-agent-2/py")
os.chdir("scalyr-agent-2")
make_directory("certs")
make_directory("bin")
make_directory("misc")
# Copy the version file. We copy it both to the root and the package root. The package copy is done down below.
shutil.copy(make_path(agent_source_root, "VERSION"), "VERSION")
# Copy the source files.
os.chdir("py")
shutil.copytree(make_path(agent_source_root, "scalyr_agent"), "scalyr_agent")
shutil.copytree(make_path(agent_source_root, "monitors"), "monitors")
os.chdir("monitors")
recursively_delete_files_by_name("README.md")
os.chdir("..")
shutil.copy(
make_path(agent_source_root, "VERSION"), os.path.join("scalyr_agent", "VERSION")
)
# Exclude certain files.
# TODO: Should probably use MANIFEST.in to do this, but don't know the Python-fu to do this yet.
#
# Don't include the tests directories. Also, don't include the .idea directory created by IDE.
recursively_delete_dirs_by_name("\.idea", "tests")
recursively_delete_files_by_name(
".*\.pyc", ".*\.pyo", ".*\.pyd", "all_tests\.py", ".*~"
)
os.chdir("..")
# Copy the config
if base_configs is not None:
config_path = base_configs
else:
config_path = "config"
shutil.copytree(make_path(agent_source_root, config_path), "config")
# Create the trusted CA root list.
os.chdir("certs")
cat_files(
glob_files(make_path(agent_source_root, "certs/*_root.pem")), "ca_certs.crt"
)
cat_files(
glob_files(make_path(agent_source_root, "certs/*_intermediate.pem")),
"intermediate_certs.pem",
)
for cert_file in glob_files(make_path(agent_source_root, "certs/*.pem")):
shutil.copy(cert_file, cert_file.split("/")[-1])
os.chdir("..")
# Misc extra files needed for some features.
os.chdir("misc")
# This docker file is needed by the `scalyr-agent-2-config --docker-create-custom-dockerfile` command. We
# put it in all distributions (not just the docker_tarball) in case a customer creates an imagine using a package.
shutil.copy(
make_path(agent_source_root, "docker/Dockerfile.custom_agent_config"),
"Dockerfile.custom_agent_config",
)
shutil.copy(
make_path(agent_source_root, "docker/Dockerfile.custom_k8s_config"),
"Dockerfile.custom_k8s_config",
)
os.chdir("..")
# Create symlinks for the two commands
os.chdir("bin")
make_soft_link("../py/scalyr_agent/agent_main.py", "scalyr-agent-2")
make_soft_link("../py/scalyr_agent/config_main.py", "scalyr-agent-2-config")
os.chdir("..")
write_to_file(get_build_info(), "build_info")
os.chdir(original_dir)
def make_directory(path):
"""Creates the specified directory including any parents that do not yet exist.
@param path: The path of the directory to create. This string can use a forward slash to separate path
components regardless of the separator character for this platform. This method will perform the necessary
conversion.
"""
converted_path = convert_path(path)
try:
os.makedirs(converted_path)
except OSError, error:
if error.errno == errno.EEXIST and os.path.isdir(converted_path):
pass
else:
raise
def make_path(parent_directory, path):
"""Returns the full path created by joining path to parent_directory.
This method is a convenience function because it allows path to use forward slashes
to separate path components rather than the platform's separator character.
@param parent_directory: The parent directory. This argument must use the system's separator character. This may be
None if path is relative to the current working directory.
@param path: The path to add to parent_directory. This should use forward slashes as the separator character,
regardless of the platform's character.
@return: The path created by joining the two with using the system's separator character.
"""
if parent_directory is None and os.path.sep == "/":
return path
if parent_directory is None:
result = ""
elif path.startswith("/"):
result = ""
else:
result = parent_directory
for path_part in path.split("/"):
if len(path_part) > 0:
result = os.path.join(result, path_part)
return result
def convert_path(path):
"""Converts the forward slashes in path to the platform's separator and returns the value.
@param path: The path to convert. This should use forward slashes as the separator character, regardless of the
platform's character.
@return: The path created by converting the forward slashes to the platform's separator.
"""
return make_path(None, path)
def make_soft_link(source, link_path):
"""Creates a soft link at link_path to source.
@param source: The path that the link will point to. This should use a forward slash as the separator, regardless
of the platform's separator.
@param link_path: The path where the link will be created. This should use a forward slash as the separator,
regardless of the platform's separator.
"""
os.symlink(convert_path(source), convert_path(link_path))
def glob_files(path):
"""Returns the paths that match the specified path glob (based on current working directory).
@param path: The path with glob wildcard characters to match. This should use a forward slash as the separator,
regardless of the platform's separator.
@return: The list of matched paths.
"""
return glob.glob(convert_path(path))
def recursively_delete_dirs_by_name(*dir_names):
"""Deletes any directories that are in the current working directory or any of its children whose file names
match the specified regular expressions.
This will recursively examine all children of the current working directory.
If a directory is found that needs to be deleted, all of it and its children are deleted.
@param dir_names: A variable number of strings containing regular expressions that should match the file names of
the directories that should be deleted.
"""
# Compile the strings into actual regular expression match objects.
matchers = []
for dir_name in dir_names:
matchers.append(re.compile(dir_name))
# Walk down the file tree, top down, allowing us to prune directories as we go.
for root, dirs, files in os.walk("."):
# The list of directories at the current level to delete.
to_remove = []
# Examine all directories at this level, see if any get a match
for dir_path in dirs:
remove_it = False
for matcher in matchers:
if matcher.match(dir_path):
remove_it = True
if remove_it:
to_remove.append(dir_path)
# Go back and delete it. Also, remove it from dirs so that we don't try to walk down it.
for remove_dir_path in to_remove:
shutil.rmtree(os.path.join(root, remove_dir_path))
dirs.remove(remove_dir_path)
def recursively_delete_files_by_name(*file_names):
"""Deletes any files that are in the current working directory or any of its children whose file names
match the specified regular expressions.
This will recursively examine all children of the current working directory.
@param file_names: A variable number of strings containing regular expressions that should match the file names of
the files that should be deleted.
"""
# Compile the strings into actual regular expression match objects.