github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/cluster_discovery.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"io"
     9  	"net/http"
    10  	"os"
    11  )
    12  
    13  const (
    14  	CDS_OFFLINE_AFTER_MILLIS = 1000 * 60 * 30 // 30 minutes
    15  	CDS_TYPE_APP             = "mattermost_app"
    16  )
    17  
    18  type ClusterDiscovery struct {
    19  	Id          string `json:"id"`
    20  	Type        string `json:"type"`
    21  	ClusterName string `json:"cluster_name"`
    22  	Hostname    string `json:"hostname"`
    23  	GossipPort  int32  `json:"gossip_port"`
    24  	Port        int32  `json:"port"`
    25  	CreateAt    int64  `json:"create_at"`
    26  	LastPingAt  int64  `json:"last_ping_at"`
    27  }
    28  
    29  func (o *ClusterDiscovery) PreSave() {
    30  	if o.Id == "" {
    31  		o.Id = NewId()
    32  	}
    33  
    34  	if o.CreateAt == 0 {
    35  		o.CreateAt = GetMillis()
    36  		o.LastPingAt = o.CreateAt
    37  	}
    38  }
    39  
    40  func (o *ClusterDiscovery) AutoFillHostname() {
    41  	// attempt to set the hostname from the OS
    42  	if len(o.Hostname) == 0 {
    43  		if hn, err := os.Hostname(); err == nil {
    44  			o.Hostname = hn
    45  		}
    46  	}
    47  }
    48  
    49  func (o *ClusterDiscovery) AutoFillIpAddress(iface string, ipAddress string) {
    50  	// attempt to set the hostname to the first non-local IP address
    51  	if len(o.Hostname) == 0 {
    52  		if len(ipAddress) > 0 {
    53  			o.Hostname = ipAddress
    54  		} else {
    55  			o.Hostname = GetServerIpAddress(iface)
    56  		}
    57  	}
    58  }
    59  
    60  func (o *ClusterDiscovery) IsEqual(in *ClusterDiscovery) bool {
    61  	if in == nil {
    62  		return false
    63  	}
    64  
    65  	if o.Type != in.Type {
    66  		return false
    67  	}
    68  
    69  	if o.ClusterName != in.ClusterName {
    70  		return false
    71  	}
    72  
    73  	if o.Hostname != in.Hostname {
    74  		return false
    75  	}
    76  
    77  	return true
    78  }
    79  
    80  func FilterClusterDiscovery(vs []*ClusterDiscovery, f func(*ClusterDiscovery) bool) []*ClusterDiscovery {
    81  	copy := make([]*ClusterDiscovery, 0)
    82  	for _, v := range vs {
    83  		if f(v) {
    84  			copy = append(copy, v)
    85  		}
    86  	}
    87  
    88  	return copy
    89  }
    90  
    91  func (o *ClusterDiscovery) IsValid() *AppError {
    92  	if !IsValidId(o.Id) {
    93  		return NewAppError("ClusterDiscovery.IsValid", "model.cluster.is_valid.id.app_error", nil, "", http.StatusBadRequest)
    94  	}
    95  
    96  	if len(o.ClusterName) == 0 {
    97  		return NewAppError("ClusterDiscovery.IsValid", "model.cluster.is_valid.name.app_error", nil, "", http.StatusBadRequest)
    98  	}
    99  
   100  	if len(o.Type) == 0 {
   101  		return NewAppError("ClusterDiscovery.IsValid", "model.cluster.is_valid.type.app_error", nil, "", http.StatusBadRequest)
   102  	}
   103  
   104  	if len(o.Hostname) == 0 {
   105  		return NewAppError("ClusterDiscovery.IsValid", "model.cluster.is_valid.hostname.app_error", nil, "", http.StatusBadRequest)
   106  	}
   107  
   108  	if o.CreateAt == 0 {
   109  		return NewAppError("ClusterDiscovery.IsValid", "model.cluster.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
   110  	}
   111  
   112  	if o.LastPingAt == 0 {
   113  		return NewAppError("ClusterDiscovery.IsValid", "model.cluster.is_valid.last_ping_at.app_error", nil, "", http.StatusBadRequest)
   114  	}
   115  
   116  	return nil
   117  }
   118  
   119  func (o *ClusterDiscovery) ToJson() string {
   120  	b, err := json.Marshal(o)
   121  	if err != nil {
   122  		return ""
   123  	}
   124  
   125  	return string(b)
   126  }
   127  
   128  func ClusterDiscoveryFromJson(data io.Reader) *ClusterDiscovery {
   129  	decoder := json.NewDecoder(data)
   130  	var me ClusterDiscovery
   131  	err := decoder.Decode(&me)
   132  	if err == nil {
   133  		return &me
   134  	}
   135  
   136  	return nil
   137  }