@@ -10,6 +10,7 @@ import (
1010 "strings"
1111 "time"
1212
13+ "crypto/tls"
1314 log "github.com/Sirupsen/logrus"
1415 "github.com/gorilla/sessions"
1516 "github.com/samalba/dockerclient"
@@ -37,6 +38,7 @@ const (
3738)
3839
3940var (
41+ ErrCannotPingRegistry = errors .New ("Cannot ping registry" )
4042 ErrLoginFailure = errors .New ("invalid username or password" )
4143 ErrAccountExists = errors .New ("account already exists" )
4244 ErrAccountDoesNotExist = errors .New ("account does not exist" )
@@ -107,6 +109,7 @@ type (
107109 RemoveRegistry (registry * shipyard.Registry ) error
108110 Registries () ([]* shipyard.Registry , error )
109111 Registry (name string ) (* shipyard.Registry , error )
112+ RegistryByAddress (addr string ) (* shipyard.Registry , error )
110113
111114 CreateConsoleSession (c * shipyard.ConsoleSession ) error
112115 RemoveConsoleSession (c * shipyard.ConsoleSession ) error
@@ -701,26 +704,67 @@ func (m DefaultManager) Node(name string) (*shipyard.Node, error) {
701704 return nil , nil
702705}
703706
704- func (m DefaultManager ) AddRegistry (registry * shipyard.Registry ) error {
705- resp , err := http .Get (fmt .Sprintf ("%s/v1/search" , registry .Addr ))
707+ func (m DefaultManager ) PingRegistry (registry * shipyard.Registry ) error {
708+
709+ // TODO: Please note the trailing forward slash / which is needed for Artifactory, else you get a 404.
710+ req , err := http .NewRequest ("GET" , fmt .Sprintf ("%s/v2/" , registry .Addr ), nil )
711+
712+ if err != nil {
713+ return err
714+ }
715+
716+ req .SetBasicAuth (registry .Username , registry .Password )
717+
718+ var tlsConfig * tls.Config
719+
720+ tlsConfig = nil
721+
722+ if registry .TlsSkipVerify {
723+ tlsConfig = & tls.Config {InsecureSkipVerify : true }
724+ }
725+
726+ // Create unsecured client
727+ trans := & http.Transport {
728+ TLSClientConfig : tlsConfig ,
729+ }
730+
731+ client := & http.Client {Transport : trans }
732+
733+ resp , err := client .Do (req )
734+
706735 if err != nil {
707736 return err
708737 }
709738 if resp .StatusCode != 200 {
710739 return errors .New (resp .Status )
711740 }
712741
713- if _ , err := r .Table (tblNameRegistries ).Insert (registry ).RunWrite (m .session ); err != nil {
742+ return nil
743+ }
744+
745+ func (m DefaultManager ) AddRegistry (registry * shipyard.Registry ) error {
746+
747+ if err := registry .InitRegistryClient (); err != nil {
714748 return err
715749 }
716750
751+ // TODO: consider not doing a test on adding the record, perhaps have a pingRegistry route that does this through API.
752+ if err := m .PingRegistry (registry ); err != nil {
753+ log .Error (err )
754+ return ErrCannotPingRegistry
755+ }
756+
757+ if _ , err := r .Table (tblNameRegistries ).Insert (registry ).RunWrite (m .session ); err != nil {
758+ return err
759+ }
717760 m .logEvent ("add-registry" , fmt .Sprintf ("name=%s endpoint=%s" , registry .Name , registry .Addr ), []string {"registry" })
718761
719762 return nil
720763}
721764
722765func (m DefaultManager ) RemoveRegistry (registry * shipyard.Registry ) error {
723766 res , err := r .Table (tblNameRegistries ).Get (registry .ID ).Delete ().Run (m .session )
767+ defer res .Close ()
724768 if err != nil {
725769 return err
726770 }
@@ -736,6 +780,7 @@ func (m DefaultManager) RemoveRegistry(registry *shipyard.Registry) error {
736780
737781func (m DefaultManager ) Registries () ([]* shipyard.Registry , error ) {
738782 res , err := r .Table (tblNameRegistries ).OrderBy (r .Asc ("name" )).Run (m .session )
783+ defer res .Close ()
739784 if err != nil {
740785 return nil , err
741786 }
@@ -745,21 +790,18 @@ func (m DefaultManager) Registries() ([]*shipyard.Registry, error) {
745790 return nil , err
746791 }
747792
748- registries := []* shipyard.Registry {}
749- for _ , r := range regs {
750- reg , err := shipyard .NewRegistry (r .ID , r .Name , r .Addr )
751- if err != nil {
752- return nil , err
793+ for _ , registry := range regs {
794+ if err := registry .InitRegistryClient (); err != nil {
795+ log .Errorf ("%s" , err .Error ())
753796 }
754-
755- registries = append (registries , reg )
756797 }
757798
758- return registries , nil
799+ return regs , nil
759800}
760801
761- func (m DefaultManager ) Registry (name string ) (* shipyard.Registry , error ) {
762- res , err := r .Table (tblNameRegistries ).Filter (map [string ]string {"name" : name }).Run (m .session )
802+ func (m DefaultManager ) Registry (id string ) (* shipyard.Registry , error ) {
803+ res , err := r .Table (tblNameRegistries ).Filter (map [string ]string {"id" : id }).Run (m .session )
804+ defer res .Close ()
763805 if err != nil {
764806 return nil , err
765807
@@ -772,12 +814,35 @@ func (m DefaultManager) Registry(name string) (*shipyard.Registry, error) {
772814 return nil , err
773815 }
774816
775- registry , err := shipyard .NewRegistry (reg .ID , reg .Name , reg .Addr )
817+ if err := reg .InitRegistryClient (); err != nil {
818+ log .Errorf ("%s" , err .Error ())
819+ return reg , err
820+ }
821+
822+ return reg , nil
823+ }
824+
825+ func (m DefaultManager ) RegistryByAddress (addr string ) (* shipyard.Registry , error ) {
826+ res , err := r .Table (tblNameRegistries ).Filter (map [string ]string {"addr" : addr }).Run (m .session )
827+ defer res .Close ()
776828 if err != nil {
777829 return nil , err
778830 }
831+ if res .IsNil () {
832+ log .Debugf ("Could not find registry with address %s" , addr )
833+ return nil , ErrRegistryDoesNotExist
834+ }
835+ var reg * shipyard.Registry
836+ if err := res .One (& reg ); err != nil {
837+ return nil , err
838+ }
839+
840+ if err := reg .InitRegistryClient (); err != nil {
841+ log .Error (err )
842+ return reg , err
843+ }
779844
780- return registry , nil
845+ return reg , nil
781846}
782847
783848func (m DefaultManager ) CreateConsoleSession (c * shipyard.ConsoleSession ) error {
0 commit comments