OLD | NEW |
---|---|
1 // Copyright 2015 The LUCI Authors. | 1 // Copyright 2015 The LUCI Authors. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 24 matching lines...) Expand all Loading... | |
35 "github.com/luci/luci-go/logdog/common/types" | 35 "github.com/luci/luci-go/logdog/common/types" |
36 "github.com/luci/luci-go/milo/api/resp" | 36 "github.com/luci/luci-go/milo/api/resp" |
37 "github.com/luci/luci-go/milo/buildsource/rawpresentation" | 37 "github.com/luci/luci-go/milo/buildsource/rawpresentation" |
38 "github.com/luci/luci-go/milo/common" | 38 "github.com/luci/luci-go/milo/common" |
39 "github.com/luci/luci-go/milo/common/model" | 39 "github.com/luci/luci-go/milo/common/model" |
40 "github.com/luci/luci-go/server/auth" | 40 "github.com/luci/luci-go/server/auth" |
41 ) | 41 ) |
42 | 42 |
43 // errNotMiloJob is returned if a Swarming task is fetched that does not self- | 43 // errNotMiloJob is returned if a Swarming task is fetched that does not self- |
44 // identify as a Milo job. | 44 // identify as a Milo job. |
45 var errNotMiloJob = errors.New("Not a Milo Job or access denied") | 45 var errNotMiloJob = errors.New("Not a Milo Job or access denied", common.CodeNoA ccess) |
46 | 46 |
47 // SwarmingTimeLayout is time layout used by swarming. | 47 // SwarmingTimeLayout is time layout used by swarming. |
48 const SwarmingTimeLayout = "2006-01-02T15:04:05.999999999" | 48 const SwarmingTimeLayout = "2006-01-02T15:04:05.999999999" |
49 | 49 |
50 // logDogFetchTimeout is the amount of time to wait while fetching a LogDog | 50 // logDogFetchTimeout is the amount of time to wait while fetching a LogDog |
51 // stream before we time out the fetch. | 51 // stream before we time out the fetch. |
52 const logDogFetchTimeout = 30 * time.Second | 52 const logDogFetchTimeout = 30 * time.Second |
53 | 53 |
54 // Swarming task states.. | 54 // Swarming task states.. |
55 const ( | 55 const ( |
(...skipping 715 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
771 for _, tag := range v { | 771 for _, tag := range v { |
772 var value string | 772 var value string |
773 parts := strings.SplitN(tag, ":", 2) | 773 parts := strings.SplitN(tag, ":", 2) |
774 if len(parts) == 2 { | 774 if len(parts) == 2 { |
775 value = parts[1] | 775 value = parts[1] |
776 } | 776 } |
777 res[parts[0]] = value | 777 res[parts[0]] = value |
778 } | 778 } |
779 return res | 779 return res |
780 } | 780 } |
781 | |
782 // BuildID is swarming's notion of a Build. See buildsource.ID. | |
783 type BuildID struct { | |
784 // (Required) The Swarming TaskID. | |
785 TaskID string | |
786 | |
787 // (Optional) The Swarming host. If empty, will use the | |
788 // milo-instance-configured swarming host. | |
789 Host string | |
790 } | |
791 | |
792 // getSwarmingHost parses the swarming hostname out of the context. If | |
793 // none is specified, get the default swarming host out of the global | |
794 // configs. | |
795 func (b *BuildID) getSwarmingHost(c context.Context) (server string, err error) { | |
796 server = b.Host | |
797 settings := common.GetSettings(c) | |
798 if settings.Swarming == nil { | |
799 err := errors.New("swarming not in settings") | |
800 logging.WithError(err).Errorf(c, "Go configure swarming in the s ettings page.") | |
801 return "", err | |
802 } | |
803 if server == "" || server == settings.Swarming.DefaultHost { | |
804 return settings.Swarming.DefaultHost, nil | |
805 } | |
806 // If it is specified, validate the hostname. | |
807 for _, hostname := range settings.Swarming.AllowedHosts { | |
808 if server == hostname { | |
809 return server, nil | |
810 } | |
811 } | |
812 return "", errors.New("unknown swarming host", common.CodeParameterError ) | |
813 } | |
814 | |
815 func (b *BuildID) validate() error { | |
816 if b.TaskID == "" { | |
817 return errors.New("no swarming task id", common.CodeParameterErr or) | |
818 } | |
819 return nil | |
820 } | |
821 | |
822 // Get implements buildsource.ID | |
823 func (b *BuildID) Get(ctx context.Context) (*resp.MiloBuild, error) { | |
Ryan Tseng
2017/07/13 22:00:46
nit: conventionally Milo uses "ctx" for router.Con
iannucci
2017/07/14 19:00:22
this is all goofy everywhere for reasons, but I'll
| |
824 if err := b.validate(); err != nil { | |
825 return nil, err | |
826 } | |
827 | |
828 hostname, err := b.getSwarmingHost(ctx) | |
829 if err != nil { | |
830 return nil, err | |
831 } | |
832 sf, err := NewProdService(ctx, hostname) | |
833 if err != nil { | |
834 return nil, err | |
835 } | |
836 | |
837 return (&BuildLoader{}).SwarmingBuildImpl(ctx, sf, b.TaskID) | |
838 } | |
839 | |
840 // GetLog implements buildsource.ID | |
841 func (b *BuildID) GetLog(ctx context.Context, logname string) (text string, clos ed bool, err error) { | |
842 if err = b.validate(); err != nil { | |
843 return | |
844 } | |
845 if logname == "" { | |
846 err = errors.New("no log name", common.CodeParameterError) | |
847 return | |
848 } | |
849 | |
850 hostname, err := b.getSwarmingHost(ctx) | |
851 if err != nil { | |
852 return | |
853 } | |
854 | |
855 sf, err := NewProdService(ctx, hostname) | |
856 if err != nil { | |
857 return | |
858 } | |
859 | |
860 return swarmingBuildLogImpl(ctx, sf, b.TaskID, logname) | |
861 } | |
OLD | NEW |