Revision 4b36944e snf-deploy/snfdeploy/__init__.py

b/snf-deploy/snfdeploy/__init__.py
16 16

  
17 17
def print_available_actions(command):
18 18

  
19
  if command == "keygen":
20
    print """
21
Usage: snf-deploy keygen [--force]
22

  
23
  Generate new ssh keys (both rsa and dsa keypairs)
24

  
25
  """
26

  
19 27
  if command == "vcluster":
20 28
    print """
21 29
Usage: snf-deploy vcluster
......
372 380
  parser.add_argument("--vnc", dest="vnc",
373 381
                      default=False, action="store_true",
374 382
                      help="Wheter virtual nodes will have a vnc console or not")
375
  parser.add_argument("-k", "--keygen", dest="keygen",
383
  parser.add_argument("--force", dest="force",
376 384
                      default=False, action="store_true",
377
                      help="Whether to create new ssh key pairs")
385
                      help="Force the creation of new ssh key pairs")
378 386

  
379 387
  parser.add_argument("-i", "--ssh-key", dest="ssh_key",
380 388
                      default=None,
......
399 407
                      choices=["packages", "vcluster", "prepare",
400 408
                               "synnefo", "backend", "ganeti",
401 409
                               "run", "cleanup", "test",
402
                               "all", "add"],
410
                               "all", "add", "keygen"],
403 411
                      help="Run on of the supported deployment commands")
404 412

  
405 413
  # available actions for the run command
......
487 495
    return ret
488 496

  
489 497

  
490
def create_keys(args, env):
498
def must_create_keys(force, env):
499
    """Check if we need to create ssh keys
500

  
501
    If force is true we are going to overide the old keys.
502
    Else if there are already generated keys to use, don't create new ones.
503

  
504
    """
505
    if force:
506
        return True
507
    d = os.path.join(env.templates, "root/.ssh")
508
    auth_keys_exists = os.path.exists(os.path.join(d, "authorized_keys"))
509
    dsa_exists = os.path.exists(os.path.join(d, "id_dsa"))
510
    dsa_pub_exists = os.path.exists(os.path.join(d, "id_dsa.pub"))
511
    rsa_exists = os.path.exists(os.path.join(d, "id_rsa"))
512
    rsa_pub_exists = os.path.exists(os.path.join(d, "id_rsa.pub"))
513
    # If any of the above doesn't exist return True
514
    return not (dsa_exists and dsa_pub_exists
515
                and rsa_exists and rsa_pub_exists
516
                and auth_keys_exists)
517

  
518

  
519
def do_create_keys(args, env):
491 520
  d = os.path.join(env.templates, "root/.ssh")
492 521
  a = os.path.join(d, "authorized_keys")
493 522
  for t in ("dsa", "rsa"):
......
541 570
  create_dir(env.run, False)
542 571
  create_dir(env.dns, False)
543 572

  
573
  # Check if there are keys to use
574
  if args.command == "keygen":
575
    if must_create_keys(args.force, env):
576
      do_create_keys(args, env)
577
      return 0
578
    else:
579
      print "Keys already existed.. aborting"
580
      return 1
581
  else:
582
    if (args.key_inject and (args.ssh_key is None)
583
        and must_create_keys(False, env)):
584
      print "No ssh keys to use. Run `snf-deploy keygen' first."
585
      return 1
586

  
544 587
  if args.command == "test":
545 588
    conf.print_config()
546 589

  
547 590
  if args.command == "cleanup":
548 591
    cleanup(args, env)
549 592

  
550
  if args.keygen:
551
    create_keys(args, env)
552

  
553 593
  if args.command == "packages":
554 594
    create_dir(env.packages, True)
555 595
    get_packages(args, env)

Also available in: Unified diff