code: def main(_):
      for name in (
          "repo",
          "branch",
          "example",
          "name",
          "gcs_workdir_base",
      ):
          value = getattr(FLAGS, name)
          if re.match(r"[^\w:/_-]", value):
              raise ValueError(
                  f'Invalid flag value: --{name}="{value}"'
              )
      example_base_directory = os.path.join(
          os.path.dirname(os.path.abspath(__file__)),
          os.path.pardir,
      )
      if not os.path.isdir(
          os.path.join(example_base_directory, FLAGS.example)
      ):
          raise ValueError(f"Could not find --example={FLAGS.example}")
      if FLAGS.connect and FLAGS.dry_run:
          raise ValueError("Cannot --connect to VM with --dry_run")
  
      vm_name = "-".join(
          [
              "flax",
              FLAGS.example,
              timestamp,
          ]
      )
      vm_name = re.sub(r"[^a-z0-9-]", "-", vm_name)
  
      startup_script = generate_startup_file(vm_name)
      launch_gce(vm_name=vm_name, startup_script=startup_script)
  
      login_args = [
          "gcloud",
          "compute",
          "ssh",
          "--project",
          FLAGS.project,
          "--zone",
          FLAGS.zone,
          vm_name,
          "--",
          "/sudo_tmux_a.sh",
      ]
  
      print("Your instance is being started...")
  
      print_howto(login_args)
  
      if FLAGS.connect or FLAGS.wait:
          login_true_args = login_args[:-1] + ["true"]
          while True:
              try:
                  result = subprocess.run(
                      login_true_args,
                      timeout=10,
                      stderr=subprocess.PIPE,
                  )
                  if result.returncode == 0:
                      break
                  stderr = result.stderr.decode("utf8")
                  if "connection refused" in stderr.lower():
                      print(
                          "(Connection refused - waiting a little longer...)"
                      )
                      time.sleep(20)
                  else:
                      raise ValueError(f"Unknown error: {stderr}")
              except ValueError as e:
                  if "HTTP 502" not in str(e):
                      raise e
                  print("(Bad Gateway - waiting a little longer...)")
                  time.sleep(20)
              except subprocess.TimeoutExpired:
                  print("(Timeout - waiting a little longer...)")
                  time.sleep(20)
          if "VM_READY_CMD" in os.environ:
              os.system(os.environ["VM_READY_CMD"])
          if FLAGS.connect:
              result = subprocess.run(login_args)
              # SSH session has cleared previous message, print it again.
              print_howto(login_args)
  
file_path: flax/examples/cloud/launch_gce.py
imports: import datetime
  import os
  import re
  import subprocess
  import time
  from typing import Sequence
  from absl import app
  from absl import flags
label_desc: Use image node_id 4 for calling a global function with example usage: main(_) without return types