golang.org/x/build@v0.0.0-20240506185731-218518f32b70/env/dragonfly-amd64/buildlet (about)

     1  #!/bin/sh
     2  # PROVIDE: buildlet
     3  # REQUIRE: NETWORKING DAEMON
     4  # BEFORE: LOGIN
     5  
     6  . /etc/rc.subr
     7  
     8  name=buildlet
     9  rcvar=buildlet_enable
    10  
    11  procname=/buildlet
    12  pidfile="/var/run/${name}.pid"
    13  command=/usr/sbin/daemon
    14  command_args="-r -fc -p ${pidfile}"
    15  command_args="${command_args} ${procname}"
    16  start_precmd="${name}_prestart"
    17  stop_cmd=":"
    18  
    19  load_rc_config $name
    20  : ${buildlet_enable:="NO"}
    21  
    22  buildlet_resetnet()
    23  {
    24  	warn "buildlet resetting network"
    25  	ifconfig vtnet0 down
    26  	sleep 1
    27  	ifconfig vtnet0 up
    28  	sleep 1
    29  	dhclient vtnet0
    30  	sleep 1
    31  }
    32  
    33  buildlet_fixnet()
    34  {
    35  	# Some fraction of the time, the VM comes up unable to deliver UDP packets.
    36  	# If we detect this situation, by host -W 3 failing to look up metadata.google.internal,
    37  	# then reset the network interface (ifup/ifdown/dhclient).
    38  	# Once is almost always enough, so 10 attempts should drive the failure rate to zero.
    39  	for i in 0 1 2 3 4 5 6 7 8 9; do
    40  		if /usr/local/bin/host -W 3 metadata.google.internal; then
    41  			return 0
    42  		fi
    43  		buildlet_resetnet
    44  	done
    45  	return 1
    46  }
    47  
    48  buildlet_prestart()
    49  {
    50  	local buildlet_url
    51  
    52  	info $(netstat -rn)
    53  	info $(cat /etc/resolv.conf)
    54  
    55  	if ! buildlet_fixnet; then
    56  		warn "cannot fix network"
    57  		poweroff
    58  		return 1
    59  	fi
    60  
    61  	buildlet_url=$(/usr/local/bin/curl -s -H "Metadata-Flavor: Google" \
    62  		http://metadata.google.internal/computeMetadata/v1/instance/attributes/buildlet-binary-url)
    63  
    64  	if [ "$buildlet_url" = "" ]; then
    65  		warn "cannot find buildlet url"
    66  		poweroff
    67  		return 1
    68  	fi
    69  
    70  	if ! /usr/local/bin/curl -o /buildlet "${buildlet_url}"; then
    71  		warn "failed to download buildlet from ${buildlet_url}"
    72  		poweroff
    73  		return 1
    74  	fi
    75  
    76  	chmod a+x /buildlet
    77  }
    78  
    79  run_rc_command "$1"