-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathandroid.py
173 lines (143 loc) · 5.02 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
"""
Android namespaced tasks
"""
from __future__ import print_function
import glob
import yaml
import os
import shutil
import sys
import distro
from distutils.dir_util import copy_tree
import invoke
from invoke import task
from invoke.exceptions import Exit
from .utils import bin_name, get_build_flags, load_release_versions, get_version
from .utils import REPO_PATH
from .build_tags import get_default_build_tags
from .go import deps, generate
# constants
BIN_PATH = os.path.join(".", "bin", "agent")
AGENT_TAG = "datadog/agent:master"
from .agent import DEFAULT_BUILD_TAGS
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, build_include=None, build_exclude=None,
development=True, precompile_only=False, skip_assets=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)
# Generating go source from templates by running go generate on ./pkg/status
generate(ctx)
build_tags = get_default_build_tags(puppy=True)
build_tags.add("android")
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 ("-i" if precompile_only 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)
outfile = "bin/agent/ddagent-{}-unsigned.apk".format(ver)
shutil.copyfile("cmd/agent/android/app/build/outputs/apk/release/app-release-unsigned.apk", outfile)
@task
def sign_apk(ctx, development=True):
"""
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, build_include=None, build_exclude=None,
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, build_include, build_exclude)
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(ctx):
# move the core check config
try:
shutil.rmtree(CORECHECK_CONFS_DIR)
except:
## it's ok if the dir is not there
pass
files = {}
files_list = []
os.makedirs(CORECHECK_CONFS_DIR)
for check in ANDROID_CORECHECKS:
srcfile = "cmd/agent/dist/conf.d/{}.d/conf.yaml.default".format(check)
tgtfile = "{}/{}.yaml".format(CORECHECK_CONFS_DIR, check)
shutil.copyfile(srcfile, tgtfile)
files_list.append("{}.yaml".format(check))
files["files"] = files_list
with open("{}/directory_manifest.yaml".format(CORECHECK_CONFS_DIR), '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 = "adb shell am startservice --es api_key {} --es hostname {} --es tags {} org.datadog.agent/.DDService".format(api_key, hostname, tags)
ctx.run(cmd)
@task
def stopservice(ctx):
cmd = "adb shell am force-stop org.datadog.agent"
ctx.run(cmd)