-
Notifications
You must be signed in to change notification settings - Fork 366
/
update.py
1778 lines (1505 loc) · 81.8 KB
/
update.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
# Windows Azure Linux Agent
#
# Copyright 2018 Microsoft Corporation
#
# 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.
#
# Requires Python 2.6+ and Openssl 1.0+
#
import glob
import json
import os
import platform
import re
import shutil
import signal
import stat
import subprocess
import sys
import time
import uuid
from datetime import datetime, timedelta
from azurelinuxagent.common import conf
from azurelinuxagent.common import logger
from azurelinuxagent.common.protocol.imds import get_imds_client
from azurelinuxagent.common.utils import fileutil, textutil
from azurelinuxagent.common.agent_supported_feature import get_supported_feature_by_name, SupportedFeatureNames
from azurelinuxagent.common.cgroupconfigurator import CGroupConfigurator
from azurelinuxagent.common.event import add_event, initialize_event_logger_vminfo_common_parameters, \
WALAEventOperation, EVENTS_DIRECTORY
from azurelinuxagent.common.exception import UpdateError, ExitException, AgentUpgradeExitException, AgentMemoryExceededException
from azurelinuxagent.common.future import ustr
from azurelinuxagent.common.osutil import get_osutil, systemd
from azurelinuxagent.common.persist_firewall_rules import PersistFirewallRulesHandler
from azurelinuxagent.common.protocol.goal_state import GoalStateSource
from azurelinuxagent.common.protocol.hostplugin import HostPluginProtocol, VmSettingsNotSupported
from azurelinuxagent.common.protocol.restapi import VMAgentUpdateStatus, VMAgentUpdateStatuses, ExtHandlerPackageList, \
VERSION_0
from azurelinuxagent.common.protocol.util import get_protocol_util
from azurelinuxagent.common.utils import shellutil
from azurelinuxagent.common.utils.archive import StateArchiver, AGENT_STATUS_FILE
from azurelinuxagent.common.utils.flexible_version import FlexibleVersion
from azurelinuxagent.common.utils.networkutil import AddFirewallRules
from azurelinuxagent.common.utils.shellutil import CommandError
from azurelinuxagent.common.version import AGENT_LONG_NAME, AGENT_NAME, AGENT_DIR_PATTERN, CURRENT_AGENT, AGENT_VERSION, \
CURRENT_VERSION, DISTRO_NAME, DISTRO_VERSION, get_lis_version, \
has_logrotate, PY_VERSION_MAJOR, PY_VERSION_MINOR, PY_VERSION_MICRO, get_daemon_version
from azurelinuxagent.ga.collect_logs import get_collect_logs_handler, is_log_collection_allowed
from azurelinuxagent.ga.collect_telemetry_events import get_collect_telemetry_events_handler
from azurelinuxagent.ga.env import get_env_handler
from azurelinuxagent.ga.exthandlers import HandlerManifest, ExtHandlersHandler, list_agent_lib_directory, \
ExtensionStatusValue, ExtHandlerStatusValue
from azurelinuxagent.ga.monitor import get_monitor_handler
from azurelinuxagent.ga.send_telemetry_events import get_send_telemetry_events_handler
AGENT_ERROR_FILE = "error.json" # File name for agent error record
AGENT_MANIFEST_FILE = "HandlerManifest.json"
AGENT_PARTITION_FILE = "partition"
CHILD_HEALTH_INTERVAL = 15 * 60
CHILD_LAUNCH_INTERVAL = 5 * 60
CHILD_LAUNCH_RESTART_MAX = 3
CHILD_POLL_INTERVAL = 60
MAX_FAILURE = 3 # Max failure allowed for agent before blacklisted
GOAL_STATE_PERIOD_EXTENSIONS_DISABLED = 5 * 60
ORPHAN_POLL_INTERVAL = 3
ORPHAN_WAIT_INTERVAL = 15 * 60
AGENT_SENTINEL_FILE = "current_version"
# This file marks that the first goal state (after provisioning) has been completed, either because it converged or because we received another goal
# state before it converged. The contents will be an instance of ExtensionsSummary. If the file does not exist then we have not finished processing
# the goal state.
INITIAL_GOAL_STATE_FILE = "initial_goal_state"
READONLY_FILE_GLOBS = [
"*.crt",
"*.p7m",
"*.pem",
"*.prv",
"ovf-env.xml"
]
class ExtensionsSummary(object):
"""
The extensions summary is a list of (extension name, extension status) tuples for the current goal state; it is
used to report changes in the status of extensions and to keep track of when the goal state converges (i.e. when
all extensions in the goal state reach a terminal state: success or error.)
The summary is computed from the VmStatus reported to blob storage.
"""
def __init__(self, vm_status=None):
if vm_status is None:
self.summary = []
self.converged = True
else:
# take the name and status of the extension if is it not None, else use the handler's
self.summary = [(o.name, o.status) for o in map(lambda h: h.extension_status if h.extension_status is not None else h, vm_status.vmAgent.extensionHandlers)]
self.summary.sort(key=lambda s: s[0]) # sort by extension name to make comparisons easier
self.converged = all(status in (ExtensionStatusValue.success, ExtensionStatusValue.error, ExtHandlerStatusValue.ready, ExtHandlerStatusValue.not_ready) for _, status in self.summary)
def __eq__(self, other):
return self.summary == other.summary
def __ne__(self, other):
return not (self == other)
def __str__(self):
return ustr(self.summary)
class AgentUpgradeType(object):
"""
Enum for different modes of Agent Upgrade
"""
Hotfix = "Hotfix"
Normal = "Normal"
def get_update_handler():
return UpdateHandler()
class UpdateHandler(object):
TELEMETRY_HEARTBEAT_PERIOD = timedelta(minutes=30)
CHECK_MEMORY_USAGE_PERIOD = timedelta(seconds=conf.get_cgroup_check_period())
def __init__(self):
self.osutil = get_osutil()
self.protocol_util = get_protocol_util()
self._is_running = True
# Member variables to keep track of the Agent AutoUpgrade
self.last_attempt_time = None
self._last_hotfix_upgrade_time = None
self._last_normal_upgrade_time = None
self.agents = []
self.child_agent = None
self.child_launch_time = None
self.child_launch_attempts = 0
self.child_process = None
self.signal_handler = None
self._last_telemetry_heartbeat = None
self._heartbeat_id = str(uuid.uuid4()).upper()
self._heartbeat_counter = 0
self._last_check_memory_usage = datetime.min
self._check_memory_usage_last_error_report = datetime.min
# VM Size is reported via the heartbeat, default it here.
self._vm_size = None
# these members are used to avoid reporting errors too frequently
self._heartbeat_update_goal_state_error_count = 0
self._update_goal_state_error_count = 0
self._update_goal_state_last_error_report = datetime.min
self._report_status_last_failed_goal_state = None
# incarnation of the last goal state that has been fully processed
# (None if no goal state has been processed)
self._last_incarnation = None
# ID of the last extensions goal state that has been fully processed (incarnation for WireServer goal states or etag for HostGAPlugin goal states)
# (None if no extensions goal state has been processed)
self._last_extensions_gs_id = None
# Goal state that is currently been processed (None if no goal state is being processed)
self._goal_state = None
# Whether the agent supports FastTrack (it does, as long as the HostGAPlugin supports the vmSettings API)
self._supports_fast_track = False
self._extensions_summary = ExtensionsSummary()
self._is_initial_goal_state = not os.path.exists(self._initial_goal_state_file_path())
if not conf.get_extensions_enabled():
self._goal_state_period = GOAL_STATE_PERIOD_EXTENSIONS_DISABLED
else:
if self._is_initial_goal_state:
self._goal_state_period = conf.get_initial_goal_state_period()
else:
self._goal_state_period = conf.get_goal_state_period()
def run_latest(self, child_args=None):
"""
This method is called from the daemon to find and launch the most
current, downloaded agent.
Note:
- Most events should be tagged to the launched agent (agent_version)
"""
if self.child_process is not None:
raise Exception("Illegal attempt to launch multiple goal state Agent processes")
if self.signal_handler is None:
self.signal_handler = signal.signal(signal.SIGTERM, self.forward_signal)
latest_agent = None if not conf.get_autoupdate_enabled() else self.get_latest_agent_greater_than_daemon(
daemon_version=CURRENT_VERSION)
if latest_agent is None:
logger.info(u"Installed Agent {0} is the most current agent", CURRENT_AGENT)
agent_cmd = "python -u {0} -run-exthandlers".format(sys.argv[0])
agent_dir = os.getcwd()
agent_name = CURRENT_AGENT
agent_version = CURRENT_VERSION
else:
logger.info(u"Determined Agent {0} to be the latest agent", latest_agent.name)
agent_cmd = latest_agent.get_agent_cmd()
agent_dir = latest_agent.get_agent_dir()
agent_name = latest_agent.name
agent_version = latest_agent.version
if child_args is not None:
agent_cmd = "{0} {1}".format(agent_cmd, child_args)
try:
# Launch the correct Python version for python-based agents
cmds = textutil.safe_shlex_split(agent_cmd)
if cmds[0].lower() == "python":
cmds[0] = sys.executable
agent_cmd = " ".join(cmds)
self._evaluate_agent_health(latest_agent)
self.child_process = subprocess.Popen(
cmds,
cwd=agent_dir,
stdout=sys.stdout,
stderr=sys.stderr,
env=os.environ)
logger.verbose(u"Agent {0} launched with command '{1}'", agent_name, agent_cmd)
# Setting the poll interval to poll every second to reduce the agent provisioning time;
# The daemon shouldn't wait for 60secs before starting the ext-handler in case the
# ext-handler kills itself during agent-update during the first 15 mins (CHILD_HEALTH_INTERVAL)
poll_interval = 1
ret = None
start_time = time.time()
while (time.time() - start_time) < CHILD_HEALTH_INTERVAL:
time.sleep(poll_interval)
try:
ret = self.child_process.poll()
except OSError:
# if child_process has terminated, calling poll could raise an exception
ret = -1
if ret is not None:
break
if ret is None or ret <= 0:
msg = u"Agent {0} launched with command '{1}' is successfully running".format(
agent_name,
agent_cmd)
logger.info(msg)
add_event(
AGENT_NAME,
version=agent_version,
op=WALAEventOperation.Enable,
is_success=True,
message=msg,
log_event=False)
if ret is None:
# Wait for the process to exit
if self.child_process.wait() > 0:
msg = u"ExtHandler process {0} launched with command '{1}' exited with return code: {2}".format(
agent_name,
agent_cmd,
ret)
logger.warn(msg)
else:
msg = u"Agent {0} launched with command '{1}' failed with return code: {2}".format(
agent_name,
agent_cmd,
ret)
logger.warn(msg)
add_event(
AGENT_NAME,
version=agent_version,
op=WALAEventOperation.Enable,
is_success=False,
message=msg)
except Exception as e:
# Ignore child errors during termination
if self.is_running:
msg = u"Agent {0} launched with command '{1}' failed with exception: \n".format(
agent_name,
agent_cmd)
logger.warn(msg)
detailed_message = '{0} {1}'.format(msg, textutil.format_exception(e))
add_event(
AGENT_NAME,
version=agent_version,
op=WALAEventOperation.Enable,
is_success=False,
message=detailed_message)
if latest_agent is not None:
latest_agent.mark_failure(is_fatal=True, reason=detailed_message)
self.child_process = None
return
def run(self, debug=False):
"""
This is the main loop which watches for agent and extension updates.
"""
try:
logger.info("{0} (Goal State Agent version {1})", AGENT_LONG_NAME, AGENT_VERSION)
logger.info("OS: {0} {1}", DISTRO_NAME, DISTRO_VERSION)
logger.info("Python: {0}.{1}.{2}", PY_VERSION_MAJOR, PY_VERSION_MINOR, PY_VERSION_MICRO)
os_info_msg = u"Distro: {dist_name}-{dist_ver}; "\
u"OSUtil: {util_name}; AgentService: {service_name}; "\
u"Python: {py_major}.{py_minor}.{py_micro}; "\
u"systemd: {systemd}; "\
u"LISDrivers: {lis_ver}; "\
u"logrotate: {has_logrotate};".format(
dist_name=DISTRO_NAME, dist_ver=DISTRO_VERSION,
util_name=type(self.osutil).__name__,
service_name=self.osutil.service_name,
py_major=PY_VERSION_MAJOR, py_minor=PY_VERSION_MINOR,
py_micro=PY_VERSION_MICRO, systemd=systemd.is_systemd(),
lis_ver=get_lis_version(), has_logrotate=has_logrotate()
)
logger.info(os_info_msg)
#
# Initialize the goal state; some components depend on information provided by the goal state and this
# call ensures the required info is initialized (e.g. telemetry depends on the container ID.)
#
protocol = self.protocol_util.get_protocol()
self._initialize_goal_state(protocol)
# Initialize the common parameters for telemetry events
initialize_event_logger_vminfo_common_parameters(protocol)
# Send telemetry for the OS-specific info.
add_event(AGENT_NAME, op=WALAEventOperation.OSInfo, message=os_info_msg)
#
# Perform initialization tasks
#
from azurelinuxagent.ga.exthandlers import get_exthandlers_handler, migrate_handler_state
exthandlers_handler = get_exthandlers_handler(protocol)
migrate_handler_state()
from azurelinuxagent.ga.remoteaccess import get_remote_access_handler
remote_access_handler = get_remote_access_handler(protocol)
self._ensure_no_orphans()
self._emit_restart_event()
self._emit_changes_in_default_configuration()
self._ensure_partition_assigned()
self._ensure_readonly_files()
self._ensure_cgroups_initialized()
self._ensure_extension_telemetry_state_configured_properly(protocol)
self._ensure_firewall_rules_persisted(dst_ip=protocol.get_endpoint())
self._add_accept_tcp_firewall_rule_if_not_enabled(dst_ip=protocol.get_endpoint())
self._reset_legacy_blacklisted_agents()
self._cleanup_legacy_goal_state_history()
# Get all thread handlers
telemetry_handler = get_send_telemetry_events_handler(self.protocol_util)
all_thread_handlers = [
get_monitor_handler(),
get_env_handler(),
telemetry_handler,
get_collect_telemetry_events_handler(telemetry_handler)
]
if is_log_collection_allowed():
all_thread_handlers.append(get_collect_logs_handler())
# Launch all monitoring threads
self._start_threads(all_thread_handlers)
logger.info("Goal State Period: {0} sec. This indicates how often the agent checks for new goal states and reports status.", self._goal_state_period)
while self.is_running:
self._check_daemon_running(debug)
self._check_threads_running(all_thread_handlers)
self._process_goal_state(exthandlers_handler, remote_access_handler)
self._send_heartbeat_telemetry(protocol)
self._check_agent_memory_usage()
time.sleep(self._goal_state_period)
except AgentUpgradeExitException as exitException:
add_event(op=WALAEventOperation.AgentUpgrade, message=exitException.reason, log_event=False)
logger.info(exitException.reason)
except ExitException as exitException:
logger.info(exitException.reason)
except Exception as error:
msg = u"Agent {0} failed with exception: {1}".format(CURRENT_AGENT, ustr(error))
self._set_sentinel(msg=msg)
logger.warn(msg)
logger.warn(textutil.format_exception(error))
sys.exit(1)
# additional return here because sys.exit is mocked in unit tests
return
self._shutdown()
sys.exit(0)
def _initialize_goal_state(self, protocol):
#
# Block until we can fetch the first goal state (self._try_update_goal_state() does its own logging and error handling).
#
while not self._try_update_goal_state(protocol):
time.sleep(conf.get_goal_state_period())
#
# If FastTrack is disabled we need to check if the current goal state (which will be retrieved using the WireServer and
# hence will be a Fabric goal state) is outdated.
#
if not conf.get_enable_fast_track():
last_fast_track_timestamp = HostPluginProtocol.get_fast_track_timestamp()
if last_fast_track_timestamp is not None:
egs = protocol.client.get_goal_state().extensions_goal_state
if egs.created_on_timestamp < last_fast_track_timestamp:
egs.is_outdated = True
logger.info("The current Fabric goal state is older than the most recent FastTrack goal state; will skip it.\nFabric: {0}\nFastTrack: {1}",
egs.created_on_timestamp, last_fast_track_timestamp)
def _get_vm_size(self, protocol):
"""
Including VMSize is meant to capture the architecture of the VM (i.e. arm64 VMs will
have arm64 included in their vmsize field and amd64 will have no architecture indicated).
"""
if self._vm_size is None:
imds_client = get_imds_client(protocol.get_endpoint())
try:
imds_info = imds_client.get_compute()
self._vm_size = imds_info.vmSize
except Exception as e:
err_msg = "Attempts to retrieve VM size information from IMDS are failing: {0}".format(textutil.format_exception(e))
logger.periodic_warn(logger.EVERY_SIX_HOURS, "[PERIODIC] {0}".format(err_msg))
return "unknown"
return self._vm_size
def _get_vm_arch(self):
return platform.machine()
def _check_daemon_running(self, debug):
# Check that the parent process (the agent's daemon) is still running
if not debug and self._is_orphaned:
raise ExitException("Agent {0} is an orphan -- exiting".format(CURRENT_AGENT))
def _start_threads(self, all_thread_handlers):
for thread_handler in all_thread_handlers:
thread_handler.run()
def _check_threads_running(self, all_thread_handlers):
# Check that all the threads are still running
for thread_handler in all_thread_handlers:
if thread_handler.keep_alive() and not thread_handler.is_alive():
logger.warn("{0} thread died, restarting".format(thread_handler.get_thread_name()))
thread_handler.start()
def _try_update_goal_state(self, protocol):
"""
Attempts to update the goal state and returns True on success or False on failure, sending telemetry events about the failures.
"""
try:
max_errors_to_log = 3
protocol.client.update_goal_state(silent=self._update_goal_state_error_count >= max_errors_to_log)
self._goal_state = protocol.get_goal_state()
if self._update_goal_state_error_count > 0:
message = u"Fetching the goal state recovered from previous errors. Fetched {0} (certificates: {1})".format(
self._goal_state.extensions_goal_state.id, self._goal_state.certs.summary)
add_event(AGENT_NAME, op=WALAEventOperation.FetchGoalState, version=CURRENT_VERSION, is_success=True, message=message, log_event=False)
logger.info(message)
self._update_goal_state_error_count = 0
try:
self._supports_fast_track = conf.get_enable_fast_track() and protocol.client.get_host_plugin().check_vm_settings_support()
except VmSettingsNotSupported:
self._supports_fast_track = False
except Exception as e:
self._update_goal_state_error_count += 1
self._heartbeat_update_goal_state_error_count += 1
if self._update_goal_state_error_count <= max_errors_to_log:
message = u"Error fetching the goal state: {0}".format(textutil.format_exception(e))
logger.error(message)
add_event(op=WALAEventOperation.FetchGoalState, is_success=False, message=message, log_event=False)
self._update_goal_state_last_error_report = datetime.now()
else:
if self._update_goal_state_last_error_report + timedelta(hours=6) > datetime.now():
self._update_goal_state_last_error_report = datetime.now()
message = u"Fetching the goal state is still failing: {0}".format(textutil.format_exception(e))
logger.error(message)
add_event(op=WALAEventOperation.FetchGoalState, is_success=False, message=message, log_event=False)
return False
return True
def __update_guest_agent(self, protocol):
"""
This function checks for new Agent updates and raises AgentUpgradeExitException if available.
There are 2 different ways the agent checks for an update -
1) Requested Version is specified in the Goal State.
- In this case, the Agent will download the requested version and upgrade/downgrade instantly.
2) No requested version.
- In this case, the agent will periodically check (1 hr) for new agent versions in GA Manifest.
- If available, it will download all versions > CURRENT_VERSION.
- Depending on the highest version > CURRENT_VERSION,
the agent will update within 4 hrs (for a Hotfix update) or 24 hrs (for a Normal update)
"""
def log_next_update_time():
next_normal_time, next_hotfix_time = self.__get_next_upgrade_times()
upgrade_type = self.__get_agent_upgrade_type(available_agent)
next_time = next_hotfix_time if upgrade_type == AgentUpgradeType.Hotfix else next_normal_time
message_ = "Discovered new {0} upgrade {1}; Will upgrade on or after {2}".format(
upgrade_type, available_agent.name,
datetime.utcfromtimestamp(next_time).strftime(logger.Logger.LogTimeFormatInUTC))
add_event(AGENT_NAME, op=WALAEventOperation.AgentUpgrade, version=CURRENT_VERSION, is_success=True,
message=message_, log_event=False)
logger.info(message_)
def handle_updates_for_requested_version():
if requested_version < CURRENT_VERSION:
prefix = "downgrade"
# In case of a downgrade, we blacklist the current agent to avoid starting it back up ever again
# (the expectation here being that if RSM is asking us to a downgrade,
# there's a good reason for not wanting the current version).
try:
# We should always have an agent directory for the CURRENT_VERSION
# (unless the CURRENT_VERSION == daemon version, but since we don't support downgrading
# below daemon version, we will never reach this code path if that's the scenario)
current_agent = next(agent for agent in self.agents if agent.version == CURRENT_VERSION)
msg = "Blacklisting the agent {0} since a downgrade was requested in the GoalState, " \
"suggesting that we really don't want to execute any extensions using this version".format(
CURRENT_VERSION)
logger.info(msg)
current_agent.mark_failure(is_fatal=True, reason=msg)
except StopIteration:
logger.warn(
"Could not find a matching agent with current version {0} to blacklist, skipping it".format(
CURRENT_VERSION))
else:
# In case of an upgrade, we don't need to blacklist anything as the daemon will automatically
# start the next available highest version which would be the requested version
prefix = "upgrade"
raise AgentUpgradeExitException(
"Exiting current process to {0} to the request Agent version {1}".format(prefix, requested_version))
# Skip the update if there is no goal state yet or auto-update is disabled
if self._goal_state is None or not conf.get_autoupdate_enabled():
return False
if self._download_agent_if_upgrade_available(protocol):
# The call to get_latest_agent_greater_than_daemon() also finds all agents in directory and sets the self.agents property.
# This state is used to find the GuestAgent object with the current version later if requested version is available in last GS.
available_agent = self.get_latest_agent_greater_than_daemon()
requested_version, _ = self.__get_requested_version_and_agent_family_from_last_gs()
if requested_version is not None:
# If requested version specified, upgrade/downgrade to the specified version instantly as this is
# driven by the goal state (as compared to the agent periodically checking for new upgrades every hour)
handle_updates_for_requested_version()
elif available_agent is None:
# Legacy behavior: The current agent can become unavailable and needs to be reverted.
# In that case, self._upgrade_available() returns True and available_agent would be None. Handling it here.
raise AgentUpgradeExitException(
"Agent {0} is reverting to the installed agent -- exiting".format(CURRENT_AGENT))
else:
log_next_update_time()
self.__upgrade_agent_if_permitted()
def _processing_new_incarnation(self):
"""
True if we are currently processing a new incarnation (i.e. WireServer goal state)
"""
return self._goal_state is not None and self._goal_state.incarnation != self._last_incarnation
def _processing_new_extensions_goal_state(self):
"""
True if we are currently processing a new extensions goal state
"""
egs = self._goal_state.extensions_goal_state
return self._goal_state is not None and egs.id != self._last_extensions_gs_id and not egs.is_outdated
def _process_goal_state(self, exthandlers_handler, remote_access_handler):
protocol = exthandlers_handler.protocol
# update self._goal_state
if not self._try_update_goal_state(protocol):
# agent updates and status reporting should be done even when the goal state is not updated
self.__update_guest_agent(protocol)
self._report_status(exthandlers_handler)
return
# check for agent updates
self.__update_guest_agent(protocol)
try:
if self._processing_new_extensions_goal_state():
if not self._extensions_summary.converged:
message = "A new goal state was received, but not all the extensions in the previous goal state have completed: {0}".format(self._extensions_summary)
logger.warn(message)
add_event(op=WALAEventOperation.GoalState, message=message, is_success=False, log_event=False)
if self._is_initial_goal_state:
self._on_initial_goal_state_completed(self._extensions_summary)
self._extensions_summary = ExtensionsSummary()
exthandlers_handler.run()
# check cgroup and disable if any extension started in agent cgroup after goal state processed.
# Note: Monitor thread periodically checks this in addition to here.
CGroupConfigurator.get_instance().check_cgroups(cgroup_metrics=[])
# report status before processing the remote access, since that operation can take a long time
self._report_status(exthandlers_handler)
if self._processing_new_incarnation():
remote_access_handler.run()
# lastly, archive the goal state history (but do it only on new goal states - no need to do it on every iteration)
if self._processing_new_extensions_goal_state():
UpdateHandler._archive_goal_state_history()
finally:
if self._goal_state is not None:
self._last_incarnation = self._goal_state.incarnation
self._last_extensions_gs_id = self._goal_state.extensions_goal_state.id
@staticmethod
def _archive_goal_state_history():
try:
archiver = StateArchiver(conf.get_lib_dir())
archiver.archive()
except Exception as exception:
logger.warn("Error cleaning up the goal state history: {0}", ustr(exception))
@staticmethod
def _cleanup_legacy_goal_state_history():
try:
StateArchiver.purge_legacy_goal_state_history()
except Exception as exception:
logger.warn("Error removing legacy history files: {0}", ustr(exception))
def __get_vmagent_update_status(self, goal_state_changed):
"""
This function gets the VMAgent update status as per the last GoalState.
Returns: None if the last GS does not ask for requested version else VMAgentUpdateStatus
"""
if not conf.get_enable_ga_versioning():
return None
update_status = None
try:
requested_version, manifest = self.__get_requested_version_and_agent_family_from_last_gs()
if manifest is None and goal_state_changed:
logger.info("Unable to report update status as no matching manifest found for family: {0}".format(
conf.get_autoupdate_gafamily()))
return None
if requested_version is not None:
if CURRENT_VERSION == requested_version:
status = VMAgentUpdateStatuses.Success
code = 0
else:
status = VMAgentUpdateStatuses.Error
code = 1
update_status = VMAgentUpdateStatus(expected_version=manifest.requested_version_string, status=status,
code=code)
except Exception as error:
if goal_state_changed:
err_msg = "[This error will only be logged once per goal state] " \
"Ran into error when trying to fetch updateStatus for the agent, skipping reporting update satus. Error: {0}".format(
textutil.format_exception(error))
logger.warn(err_msg)
add_event(op=WALAEventOperation.AgentUpgrade, is_success=False, message=err_msg, log_event=False)
return update_status
def _report_status(self, exthandlers_handler):
vm_agent_update_status = self.__get_vmagent_update_status(self._processing_new_extensions_goal_state())
# report_ext_handlers_status does its own error handling and returns None if an error occurred
vm_status = exthandlers_handler.report_ext_handlers_status(
goal_state_changed=self._processing_new_extensions_goal_state(),
vm_agent_update_status=vm_agent_update_status, vm_agent_supports_fast_track=self._supports_fast_track)
if vm_status is not None:
self._report_extensions_summary(vm_status)
if self._goal_state is not None:
agent_status = exthandlers_handler.get_ext_handlers_status_debug_info(vm_status)
self._goal_state.save_to_history(agent_status, AGENT_STATUS_FILE)
if self._goal_state.extensions_goal_state.is_outdated:
exthandlers_handler.protocol.client.get_host_plugin().clear_fast_track_state()
def _report_extensions_summary(self, vm_status):
try:
extensions_summary = ExtensionsSummary(vm_status)
if self._extensions_summary != extensions_summary:
self._extensions_summary = extensions_summary
message = "Extension status: {0}".format(self._extensions_summary)
logger.info(message)
add_event(op=WALAEventOperation.GoalState, message=message, is_success=True)
if self._extensions_summary.converged:
message = "All extensions in the goal state have reached a terminal state: {0}".format(extensions_summary)
logger.info(message)
add_event(op=WALAEventOperation.GoalState, message=message, is_success=True)
if self._is_initial_goal_state:
self._on_initial_goal_state_completed(self._extensions_summary)
except Exception as error:
# report errors only once per goal state
if self._report_status_last_failed_goal_state != self._goal_state.extensions_goal_state.id:
self._report_status_last_failed_goal_state = self._goal_state.extensions_goal_state.id
msg = u"Error logging the goal state summary: {0}".format(textutil.format_exception(error))
logger.warn(msg)
add_event(op=WALAEventOperation.GoalState, is_success=False, message=msg)
def _on_initial_goal_state_completed(self, extensions_summary):
fileutil.write_file(self._initial_goal_state_file_path(), ustr(extensions_summary))
if conf.get_extensions_enabled() and self._goal_state_period != conf.get_goal_state_period():
self._goal_state_period = conf.get_goal_state_period()
logger.info("Initial goal state completed, switched the goal state period to {0}", self._goal_state_period)
self._is_initial_goal_state = False
def forward_signal(self, signum, frame):
if signum == signal.SIGTERM:
self._shutdown()
if self.child_process is None:
return
logger.info(
u"Agent {0} forwarding signal {1} to {2}\n",
CURRENT_AGENT,
signum,
self.child_agent.name if self.child_agent is not None else CURRENT_AGENT)
self.child_process.send_signal(signum)
if self.signal_handler not in (None, signal.SIG_IGN, signal.SIG_DFL):
self.signal_handler(signum, frame)
elif self.signal_handler is signal.SIG_DFL:
if signum == signal.SIGTERM:
self._shutdown()
sys.exit(0)
return
@staticmethod
def __get_daemon_version_for_update():
daemon_version = get_daemon_version()
if daemon_version != FlexibleVersion(VERSION_0):
return daemon_version
# We return 0.0.0.0 if daemon version is not specified. In that case,
# use the min version as 2.2.53 as we started setting the daemon version starting 2.2.53.
return FlexibleVersion("2.2.53")
def get_latest_agent_greater_than_daemon(self, daemon_version=None):
"""
If autoupdate is enabled, return the most current, downloaded,
non-blacklisted agent which is not the current version (if any) and is greater than the `daemon_version`.
Otherwise, return None (implying to use the installed agent).
If `daemon_version` is None, we fetch it from the environment variable set by the DaemonHandler
"""
self._find_agents()
daemon_version = self.__get_daemon_version_for_update() if daemon_version is None else daemon_version
# Fetch the downloaded agents that are different from the current version and greater than the daemon version
available_agents = [agent for agent in self.agents
if agent.is_available
and agent.version != CURRENT_VERSION and agent.version > daemon_version]
return available_agents[0] if len(available_agents) >= 1 else None
def _emit_restart_event(self):
try:
if not self._is_clean_start:
msg = u"Agent did not terminate cleanly: {0}".format(
fileutil.read_file(self._sentinel_file_path()))
logger.info(msg)
add_event(
AGENT_NAME,
version=CURRENT_VERSION,
op=WALAEventOperation.Restart,
is_success=False,
message=msg)
except Exception:
pass
return
@staticmethod
def _emit_changes_in_default_configuration():
try:
def log_event(msg):
logger.info("******** {0} ********", msg)
add_event(AGENT_NAME, op=WALAEventOperation.ConfigurationChange, message=msg)
def log_if_int_changed_from_default(name, current, message=""):
default = conf.get_int_default_value(name)
if default != current:
log_event("{0} changed from its default: {1}. New value: {2}. {3}".format(name, default, current, message))
def log_if_op_disabled(name, value):
if not value:
log_event("{0} is set to False, not processing the operation".format(name))
log_if_int_changed_from_default("Extensions.GoalStatePeriod", conf.get_goal_state_period(),
"Changing this value affects how often extensions are processed and status for the VM is reported. Too small a value may report the VM as unresponsive")
log_if_int_changed_from_default("Extensions.InitialGoalStatePeriod", conf.get_initial_goal_state_period(),
"Changing this value affects how often extensions are processed and status for the VM is reported. Too small a value may report the VM as unresponsive")
log_if_op_disabled("OS.EnableFirewall", conf.enable_firewall())
log_if_op_disabled("Extensions.Enabled", conf.get_extensions_enabled())
log_if_op_disabled("AutoUpdate.Enabled", conf.get_autoupdate_enabled())
if conf.enable_firewall():
log_if_int_changed_from_default("OS.EnableFirewallPeriod", conf.get_enable_firewall_period())
if conf.get_autoupdate_enabled():
log_if_int_changed_from_default("Autoupdate.Frequency", conf.get_autoupdate_frequency())
if conf.get_enable_fast_track():
log_if_op_disabled("Debug.EnableFastTrack", conf.get_enable_fast_track())
if conf.get_lib_dir() != "/var/lib/waagent":
log_event("lib dir is in an unexpected location: {0}".format(conf.get_lib_dir()))
except Exception as e:
logger.warn("Failed to log changes in configuration: {0}", ustr(e))
def _ensure_no_orphans(self, orphan_wait_interval=ORPHAN_WAIT_INTERVAL):
pid_files, ignored = self._write_pid_file() # pylint: disable=W0612
for pid_file in pid_files:
try:
pid = fileutil.read_file(pid_file)
wait_interval = orphan_wait_interval
while self.osutil.check_pid_alive(pid):
wait_interval -= ORPHAN_POLL_INTERVAL
if wait_interval <= 0:
logger.warn(
u"{0} forcibly terminated orphan process {1}",
CURRENT_AGENT,
pid)
os.kill(pid, signal.SIGKILL)
break
logger.info(
u"{0} waiting for orphan process {1} to terminate",
CURRENT_AGENT,
pid)
time.sleep(ORPHAN_POLL_INTERVAL)
os.remove(pid_file)
except Exception as e:
logger.warn(
u"Exception occurred waiting for orphan agent to terminate: {0}",
ustr(e))
return
def _ensure_partition_assigned(self):
"""
Assign the VM to a partition (0 - 99). Downloaded updates may be configured
to run on only some VMs; the assigned partition determines eligibility.
"""
if not os.path.exists(self._partition_file):
partition = ustr(int(datetime.utcnow().microsecond / 10000))
fileutil.write_file(self._partition_file, partition)
add_event(
AGENT_NAME,
version=CURRENT_VERSION,
op=WALAEventOperation.Partition,
is_success=True,
message=partition)
def _ensure_readonly_files(self):
for g in READONLY_FILE_GLOBS:
for path in glob.iglob(os.path.join(conf.get_lib_dir(), g)):
os.chmod(path, stat.S_IRUSR)
def _ensure_cgroups_initialized(self):
configurator = CGroupConfigurator.get_instance()
configurator.initialize()
def _evaluate_agent_health(self, latest_agent):
"""
Evaluate the health of the selected agent: If it is restarting
too frequently, raise an Exception to force blacklisting.
"""
if latest_agent is None:
self.child_agent = None
return
if self.child_agent is None or latest_agent.version != self.child_agent.version:
self.child_agent = latest_agent
self.child_launch_time = None
self.child_launch_attempts = 0
if self.child_launch_time is None:
self.child_launch_time = time.time()
self.child_launch_attempts += 1
if (time.time() - self.child_launch_time) <= CHILD_LAUNCH_INTERVAL \
and self.child_launch_attempts >= CHILD_LAUNCH_RESTART_MAX:
msg = u"Agent {0} restarted more than {1} times in {2} seconds".format(
self.child_agent.name,
CHILD_LAUNCH_RESTART_MAX,
CHILD_LAUNCH_INTERVAL)
raise Exception(msg)
return
def _filter_blacklisted_agents(self):
self.agents = [agent for agent in self.agents if not agent.is_blacklisted]
def _find_agents(self):
"""
Load all non-blacklisted agents currently on disk.
"""
try:
self._set_and_sort_agents(self._load_agents())
self._filter_blacklisted_agents()
except Exception as e:
logger.warn(u"Exception occurred loading available agents: {0}", ustr(e))
return
def _get_pid_parts(self):
pid_file = conf.get_agent_pid_file_path()
pid_dir = os.path.dirname(pid_file)
pid_name = os.path.basename(pid_file)
pid_re = re.compile("(\d+)_{0}".format(re.escape(pid_name))) # pylint: disable=W1401
return pid_dir, pid_name, pid_re
def _get_pid_files(self):
pid_dir, pid_name, pid_re = self._get_pid_parts() # pylint: disable=W0612
pid_files = [os.path.join(pid_dir, f) for f in os.listdir(pid_dir) if pid_re.match(f)]
pid_files.sort(key=lambda f: int(pid_re.match(os.path.basename(f)).group(1)))
return pid_files
@property
def is_running(self):
return self._is_running
@is_running.setter
def is_running(self, value):
self._is_running = value
@property
def _is_clean_start(self):
return not os.path.isfile(self._sentinel_file_path())
@property
def _is_orphaned(self):
parent_pid = os.getppid()
if parent_pid in (1, None):
return True
if not os.path.isfile(conf.get_agent_pid_file_path()):
return True
return fileutil.read_file(conf.get_agent_pid_file_path()) != ustr(parent_pid)
def _load_agents(self):
path = os.path.join(conf.get_lib_dir(), "{0}-*".format(AGENT_NAME))
return [GuestAgent.from_installed_agent(agent_dir)
for agent_dir in glob.iglob(path) if os.path.isdir(agent_dir)]
def _partition(self):
return int(fileutil.read_file(self._partition_file))
@property
def _partition_file(self):
return os.path.join(conf.get_lib_dir(), AGENT_PARTITION_FILE)