fork/home-manager
git clone https://git.julia.blue/fork/home-manager
223e3c38
master
1require 'set' 2require 'open3' 3 4@dry_run = ENV [ 'DRY_RUN' ] 5@verbose = ENV [ 'VERBOSE' ] 6 7UnitsDir = 'home-files/.config/systemd/user' 8 9# 1. Stop all services from the old generation that are not present in the new generation. 10# 2. Ensure all services from the new generation that are wanted by active targets are running: 11# - Start services that are not already running. 12# - Restart services whose unit config files have changed between generations. 13# 3. If any services were (re)started, wait 'start_timeout_ms' and report services 14# that failed to start. This helps debugging quickly failing services. 15# 16# Whenever service failures are detected, show the output of 17# 'systemd --user status' for the affected services. 18# 19def setup_services ( old_gen_path , new_gen_path , start_timeout_ms_string ) 20start_timeout_ms = start_timeout_ms_string . to_i 21 22old_units_path = File . join ( old_gen_path , UnitsDir ) unless old_gen_path . empty? 23new_units_path = File . join ( new_gen_path , UnitsDir ) 24 25old_services = get_services ( old_units_path ) 26new_services = get_services ( new_units_path ) 27 28exit if old_services . empty? &&new_services . empty? 29 30all_services = get_active_targets_units ( new_units_path ) 31maybe_changed = all_services &old_services 32changed_services = get_changed_services ( old_units_path , new_units_path , maybe_changed ) 33unchanged_oneshots = get_oneshot_services ( maybe_changed -changed_services ) 34 35# These services should be running when this script is finished 36services_to_run = all_services -unchanged_oneshots 37 38# Only stop active services, otherwise we might get a 'service not loaded' error 39# for inactive services that were removed in the current generation. 40to_stop = get_active_units ( old_services -new_services ) 41to_restart = changed_services 42to_start = get_inactive_units ( services_to_run -to_restart ) 43 44raise "daemon-reload failed" unless run_cmd ( 'systemctl' , '--user' , 'daemon-reload' ) 45 46# Exclude units that shouldn't be (re)started or stopped 47no_manual_start , no_manual_stop , no_restart = get_restricted_units ( to_stop +to_restart +to_start ) 48notify_skipped_units ( to_restart &no_restart ) 49to_stop -=no_manual_stop 50to_restart -=no_manual_stop +no_manual_start +no_restart 51to_start -=no_manual_start 52 53if to_stop . empty? &&to_start . empty? &&to_restart . empty? 54print_service_msg ( "All services are already running" , services_to_run ) 55else 56puts "Setting up services" if @verbose 57systemctl_action ( 'stop' , to_stop ) 58systemctl_action ( 'start' , to_start ) 59systemctl_action ( 'restart' , to_restart ) 60started_services = to_start +to_restart 61if start_timeout_ms >0 && !started_services . empty? && !@dry_run 62failed = wait_and_get_failed_services ( started_services , start_timeout_ms ) 63if failed . empty? 64print_service_msg ( "All services are running" , services_to_run ) 65else 66puts 67puts "Error. These services failed to start:" , failed 68show_failed_services_status ( failed ) 69exit 1 70end 71end 72end 73end 74 75def get_services ( dir ) 76services = get_service_files ( dir ) if dir &&Dir . exists? ( dir ) 77Set . new ( services ) 78end 79 80def get_service_files ( dir ) 81Dir . chdir ( dir ) { Dir [ '*[^@].{service,socket,timer}' ] } 82end 83 84def get_changed_services ( dir_a , dir_b , services ) 85services . select do |service | 86a = File . join ( dir_a , service ) 87b = File . join ( dir_b , service ) 88( File . size ( a ) !=File . size ( b )) ||( File . read ( a ) !=File . read ( b )) 89end 90end 91 92TargetDirRegexp = /^(.*\.target)\.wants$/ 93 94# @return all units wanted by active targets 95def get_active_targets_units ( units_dir ) 96return Set . new unless Dir . exists? ( units_dir ) 97targets = Dir . entries ( units_dir ). map { |entry |entry [ TargetDirRegexp , 1 ] }. compact 98active_targets = get_active_units ( targets ) 99active_units = active_targets . map do |target | 100get_service_files ( File . join ( units_dir , " #{ target } .wants" )) 101end . flatten 102Set . new ( active_units ) 103end 104 105# @return true on success 106def run_cmd ( *cmd ) 107print_cmd cmd 108@dry_run ||system ( *cmd ) 109end 110 111def systemctl_action ( cmd , services ) 112return if services . empty? 113 114verb = ( cmd =='stop' ) ?'Stopping' :" #{ cmd . capitalize } ing" 115puts " #{ verb } : #{ services . join ( ' ' )} " 116 117cmd = [ 'systemctl' , '--user' , cmd , *services ] 118if @dry_run 119puts cmd . join ( ' ' ) 120return 121end 122 123output , status = Open3 . capture2e ( *cmd ) 124output 125# Show status for failed services 126unless status . success? 127# Due to a bug in systemd, the '--user' argument is not always provided 128output . scan ( /systemctl (?:--user )?(status .*?)['"]/ ). flatten . each do |status_cmd | 129puts 130run_cmd ( "systemctl --user #{ status_cmd } " ) 131end 132exit 1 133end 134end 135 136def systemctl ( *cmd ) 137output , _ = Open3 . capture2 ( 'systemctl' , '--user' , *cmd ) 138output 139end 140 141def print_cmd ( cmd ) 142puts [ *cmd ]. join ( ' ' ) if @verbose ||@dry_run 143end 144 145def get_active_units ( units ) 146filter_units ( units ) { |state |state =='active' } 147end 148 149def get_inactive_units ( units ) 150filter_units ( units ) { |state |state !='active' } 151end 152 153def get_failed_units ( units ) 154filter_units ( units ) { |state |state =='failed' } 155end 156 157def filter_units ( units ) 158return [] if units . empty? 159states = systemctl ( 'is-active' , *units ). split 160units . select . with_index { |_ , i |yield states [ i ] } 161end 162 163def get_oneshot_services ( units ) 164return [] if units . empty? 165types = systemctl ( 'show' , '-p' , 'Type' , *units ). split 166units . select . with_index do |_ , i | 167types [ i ] =='Type=oneshot' 168end 169end 170 171def get_restricted_units ( units ) 172infos = systemctl ( 'show' , '-p' , 'RefuseManualStart' , '-p' , 'RefuseManualStop' , *units ) 173. split ( "\n\n" ) 174no_manual_start = [] 175no_manual_stop = [] 176infos . zip ( units ). each do |info , unit | 177no_start , no_stop = info . split ( "\n" ) 178no_manual_start <<unit if no_start . end_with? ( 'yes' ) 179no_manual_stop <<unit if no_stop . end_with? ( 'yes' ) 180end 181# Get units that should not be restarted even if a change has been detected. 182no_restart_regexp = /^\s*X-RestartIfChanged\s*=\s*false\b/ 183no_restart = units . select { |unit |systemctl ( 'cat' , unit ) =~no_restart_regexp } 184[ no_manual_start , no_manual_stop , no_restart ] 185end 186 187def wait_and_get_failed_services ( services , start_timeout_ms ) 188puts "Waiting #{ start_timeout_ms } ms for services to fail" 189# Force the previous message to always be visible before sleeping 190STDOUT . flush 191sleep ( start_timeout_ms /1000.0 ) 192get_failed_units ( services ) 193end 194 195def show_failed_services_status ( services ) 196puts 197services . each do |service | 198run_cmd ( 'systemctl' , '--user' , 'status' , service ) 199puts 200end 201end 202 203def print_service_msg ( msg , services ) 204return if services . empty? 205if @verbose 206puts " #{ msg } :" , services . to_a 207else 208puts msg 209end 210end 211 212def notify_skipped_units ( no_restart ) 213puts "Not restarting: #{ no_restart . join ( ' ' )} " unless no_restart . empty? 214end 215 216setup_services ( *ARGV )