-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathandroid.py
175 lines (139 loc) · 5.42 KB
/
android.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
"""
Android namespaced tasks
"""
import os
import shutil
import sys
import yaml
from invoke import task
from .build_tags import get_default_build_tags
from .utils import REPO_PATH, bin_name, get_build_flags, get_version
# constants
BIN_PATH = os.path.join(".", "bin", "agent")
AGENT_TAG = "datadog/agent:master"
ANDROID_CORECHECKS = [
"cpu",
"disk",
"io",
"load",
"memory",
"network",
"uptime",
]
CORECHECK_CONFS_DIR = "cmd/agent/android/app/src/main/assets/conf.d"
@task
def build(ctx, rebuild=False, race=False, major_version='7', python_runtimes='3'):
"""
Build the android apk. If the bits to include in the build are not specified,
the values from `invoke.yaml` will be used.
Example invokation:
inv android.build
"""
# ensure BIN_PATH exists
if not os.path.exists(BIN_PATH):
os.makedirs(BIN_PATH)
# put the check confs in place
assetconfigs(ctx)
ldflags, gcflags, env = get_build_flags(ctx, major_version=major_version, python_runtimes=python_runtimes)
build_tags = get_default_build_tags(build="android")
# Even though gomobile supports modules, I didn't manage to make `gomobile bind` work.
# There are two problems: First, it calls `go list -m all` to parse the repo dependencies,
# which doesn't work if using vendoring. This can be solved by using go modules not vendored.
# The second problem is that it tries to import our repo as a module from generated code and
# our repo is not importable due to the dependency on k8s.io/kubernetes. A workaround is to
# remove the k8s dependencies from go.mod before doing the build (since they are not needed
# for Android), but given all these problems I decided to just disable go modules.
env["GO111MODULE"] = "off"
# Install gomobile on the GOPATH instead of as a module
ctx.run('go get golang.org/x/mobile/cmd/gomobile', env=env)
# Pin its version to what's indicated in go.mod
ctx.run(
'VERSION=$(grep golang.org/x/mobile go.mod | rev | cut -d - -f 1 | rev); cd /go/src/golang.org/x/mobile; git checkout $VERSION'
)
ctx.run('go run golang.org/x/mobile/cmd/gomobile init', env=env)
cmd = "go run golang.org/x/mobile/cmd/gomobile bind -target android {race_opt} {build_type} -tags \"{go_build_tags}\" "
cmd += "-o {agent_bin} -gcflags=\"{gcflags}\" -ldflags=\"{ldflags}\" {REPO_PATH}/cmd/agent/android"
args = {
"race_opt": "-race" if race else "",
"build_type": "-a" if rebuild else "",
"go_build_tags": " ".join(build_tags),
"agent_bin": os.path.join(BIN_PATH, bin_name("ddagent", android=True)),
"gcflags": gcflags,
"ldflags": ldflags,
"REPO_PATH": REPO_PATH,
}
ctx.run(cmd.format(**args), env=env)
pwd = os.getcwd()
os.chdir("cmd/agent/android")
if sys.platform == 'win32':
cmd = "gradlew.bat --no-daemon build"
else:
cmd = "./gradlew --no-daemon build"
ctx.run(cmd)
os.chdir(pwd)
ver = get_version(ctx, include_git=True, git_sha_length=7, major_version=major_version, include_pipeline_id=True)
outfile = f"bin/agent/ddagent-{ver}-unsigned.apk"
shutil.copyfile("cmd/agent/android/app/build/outputs/apk/release/app-release-unsigned.apk", outfile)
@task
def sign_apk(ctx):
"""
Signs the APK with the default platform signature.
"""
cmd = "java -jar signapk.jar platform.x509.pem platform.pk8 bin/agent/ddagent-release-unsigned.apk bin/agent/ddagent-release-signed.apk"
ctx.run(cmd)
@task
def install(ctx, rebuild=False, race=False, skip_build=False):
"""
Installs the APK on a device.
By default it builds the agent before executing it, unless --skip-build was
passed. It accepts the same set of options as agent.build.
"""
if not skip_build:
build(ctx, rebuild, race)
sign_apk(ctx)
cmd = "adb install -r bin/agent/ddagent-release-signed.apk"
ctx.run(cmd)
@task
def clean(ctx):
"""
Remove temporary objects and binary artifacts
"""
# go clean
print("Executing go clean")
ctx.run("go clean")
# remove the bin/agent folder
print("Remove agent binary folder")
shutil.rmtree("./bin/agent")
shutil.rmtree(CORECHECK_CONFS_DIR)
@task
def assetconfigs(_):
# move the core check config
shutil.rmtree(CORECHECK_CONFS_DIR, ignore_errors=True)
files = {}
files_list = []
os.makedirs(CORECHECK_CONFS_DIR)
for check in ANDROID_CORECHECKS:
srcfile = f"cmd/agent/dist/conf.d/{check}.d/conf.yaml.default"
tgtfile = f"{CORECHECK_CONFS_DIR}/{check}.yaml"
shutil.copyfile(srcfile, tgtfile)
files_list.append(f"{check}.yaml")
files["files"] = files_list
with open(f"{CORECHECK_CONFS_DIR}/directory_manifest.yaml", 'w') as outfile:
yaml.dump(files, outfile, default_flow_style=False)
@task
def launchservice(ctx, api_key, hostname=None, tags=None):
if api_key is None:
print("must supply api key")
return
if hostname is None:
print("Setting hostname to android-tablet")
hostname = "android-tablet"
if tags is None:
print("Setting tags to owner:db,env:local,role:windows")
tags = "owner:db,env:local,role:windows"
cmd = f"adb shell am startservice --es api_key {api_key} --es hostname {hostname} --es tags {tags} org.datadog.agent/.DDService"
ctx.run(cmd)
@task
def stopservice(ctx):
cmd = "adb shell am force-stop org.datadog.agent"
ctx.run(cmd)