func main() { flag.Parse() if config.address == "" { glog.Errorf("You have not specified the ip address of the client to remove lock ownership") os.Exit(1) } // step: grab a rbd interface client, err := rbd.NewRBDInterface() if err != nil { glog.Errorf("Failed to create a client interface for rbd, error: %s", err) os.Exit(1) } err = client.UnlockClient(config.address) if err != nil { glog.Errorf("Failed to unlock the images held by %s", config.address) os.Exit(1) } glog.Infof("Successfully remove any locks") }
func main() { var err error flag.Parse() glog.Infof("Starting the %s Service, version: %s, git+sha: %s", Prog, Version, GitSha) // step: create the channel to termination requests signalChannel := make(chan os.Signal) signal.Notify(signalChannel, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) if config.envTag == "" { fmt.Printf("[error] you need to specify the environment tag for the instances are interested in") os.Exit(1) } // step: create a interface for events eventsClient, err = aws.NewEC2EventsInterface(config.aws_api_key, config.aws_api_secret, config.aws_region, config.envTag) if err != nil { glog.Errorf("Failed to start service, error: %s", err) os.Exit(1) } // step: create the rbd interface rbdClient, err = rbd.NewRBDInterface() if err != nil { glog.Errorf("Failed to create interface to rbd command set, error: %s", err) os.Exit(1) } // step: create the event channels terminatedCh := eventsClient.AddEventListener(aws.STATUS_TERMINATED) stoppedCh := eventsClient.AddEventListener(aws.STATUS_STOPPED) runningCh := eventsClient.AddEventListener(aws.STATUS_RUNNING) // step: get a list of running hosts and their ip addresses hosts = eventsClient.GetRunningHosts() // step: enter the event loop: we are either listening to a termination signal, a terminated box // or a box being added for { select { // we have received a kill service signal case <-signalChannel: glog.Infof("Recieved a shutdown signal, exiting service") os.Exit(0) case event := <-runningCh: ev := (*aws.InstanceEvent)(event) glog.Infof("Region has a new instance running, instanceId: %s", ev.InstanceID) // add to the hosts map hosts[ev.Instance.InstanceId] = ev.Instance.PrivateIpAddress case event := <-stoppedCh: ev := (*aws.InstanceEvent)(event) glog.Infof("Region instance stopped, instanceId: %s", ev.InstanceID) go removeRBDLocks(&ev.Instance) case event := <-terminatedCh: ev := (*aws.InstanceEvent)(event) glog.Infof("Region instance terminated, instanceId: %s", ev.InstanceID) go removeRBDLocks(&ev.Instance) } } }